gpt4 book ai didi

Angular 5,HttpClient 将日期字段更改为 UTC

转载 作者:太空狗 更新时间:2023-10-29 17:30:32 25 4
gpt4 key购买 nike

我有一个对象,它在客户端有一个 Date 类型的属性。当我尝试通过 HttpClient.post 将对象发送到服务器时,属性值更改为 UTC 时区。

客户端的值是 Sun Nov 26 2017 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;}

我在填写 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));

最佳答案

使用 HttpInterceptor 修改 put/post 请求的请求体。通过创建更正的日期对象递归更新所有日期字段。

示例代码:

@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);
}
}
}

关于Angular 5,HttpClient 将日期字段更改为 UTC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47495948/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com