h.j*_*ade 5 date angular angular-httpclient
我有一个客户端具有日期类型属性的对象.当我尝试将对象发送HttpClient.post到服务器时,属性的值将更改为UTC时区.
客户端值为2017年11月26日星期日00:00:00 GMT + 0300(土耳其标准时间),但当它进入服务器时,更改为:25.11.2017 21:00:00
我该怎么控制呢?
这是我的班级.
export interface IBill {
BillID : number;
SubscriptionID:number;
SiteID : number;
SubscriptionName: string;
Amount: number;
Date: Date;
PaymentDeadline: Date;
Virtual: boolean;
Portioned: boolean;
Issuanced: boolean;
FinancialTransactionComment : string;}
Run Code Online (Sandbox Code Playgroud)
我在填写ng-form时创建了一个对象,然后调用Http.post:
let bill = this.formData;
this.http.post(this.configuration.ServerWithApiUrl + url, bill , { headers: headers, withCredentials: true, observe: "response", responseType: 'text' })
.map((response) => {
return this.parseResponse(response);
}).catch(
(err) =>
this.handleError(err));
Run Code Online (Sandbox Code Playgroud)
我将日期类型,PaymentDeadline更改为字符串.
export interface IBill {
BillID : number;
SubscriptionID:number;
SiteID : number;
SubscriptionName: string;
Amount: number;
Date: string;
PaymentDeadline: string;
Virtual: boolean;
Portioned: boolean;
Issuanced: boolean;
FinancialTransactionComment : string; }
Run Code Online (Sandbox Code Playgroud)
在发送到服务之前重写它们.
let bill = this.formData;
bill.Date = this.formData.Date.toLocaleString();
bill.PaymentDeadline = this.formData.PaymentDeadline.toLocaleString();
Run Code Online (Sandbox Code Playgroud)
在这种情况下,时间将以字符串形式发送("11/10/2017,12:00:00 AM"),并且不会对UTC时区进行任何更改
小智 5
使用HttpInterceptor修改放置/发布请求的请求正文。通过创建更正的Date对象来递归更新所有日期字段。
样例代码:
@Injectable() export class UpdateDateHttpInterceptor implements HttpInterceptor {
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
this.startLoading();
if (req.method === 'POST' || req.method === 'PUT') {
this.shiftDates(req.body);
}
}
shiftDates(body) {
if (body === null || body === undefined) {
return body;
}
if (typeof body !== 'object') {
return body;
}
for (const key of Object.keys(body)) {
const value = body[key];
if (value instanceof Date) {
body[key] = new Date(Date.UTC(value.getFullYear(), value.getMonth(), value.getDate(), value.getHours(), value.getMinutes()
, value.getSeconds()));
} else if (typeof value === 'object') {
this.shiftDates(value);
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3896 次 |
| 最近记录: |