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

Angular 第5步,HttpClient将日期字段更改为UTC,angular,date,angular-httpclient,Angular,Date,Angular Httpclient,我有一个对象,它在客户端具有日期类型属性。当我尝试通过HttpClient.post向服务器发送对象时,属性的值更改为UTC时区 客户端值为2017年11月26日星期日00:00:00 GMT+0300(土耳其标准时间),但当它转到服务器时,更改为:2017年11月25日21:00:00 我怎样才能控制这一切 这是我的课 export interface IBill { BillID : number; SubscriptionID:number; SiteID : numbe

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

客户端值为2017年11月26日星期日00:00:00 GMT+0300(土耳其标准时间),但当它转到服务器时,更改为:2017年11月25日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表单时创建了此对象,然后调用以下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));

我将日期、付款截止日期的类型更改为字符串

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; }
在发送到服务之前,请重写它们

let bill = this.formData;
bill.Date = this.formData.Date.toLocaleString();
bill.PaymentDeadline = this.formData.PaymentDeadline.toLocaleString();

在这种情况下,时间将作为字符串发送(“2017年10月11日,12:00:00 AM”),UTC时区将不做任何更改

使用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);
        }
    }
  }
@Injectable()导出类UpdateDateHttpInterceptor实现HttpInterceptor{
截取(req:HttpRequest,next:HttpHandler):可观察{
这是令人吃惊的;
如果(请求方法=='POST'| |请求方法==='PUT'){
该移位日期(要求正文);
}
}
移位日期(主体){
if(body==null | | body==undefined){
返回体;
}
如果(主体类型!==“对象”){
返回体;
}
for(对象的常量键。键(主体)){
常量值=主体[键];
if(值instanceof Date){
body[key]=新日期(Date.UTC(value.getFullYear()、value.getMonth()、value.getDate()、value.getHours()、value.getMinutes())
,value.getSeconds());
}else if(typeof value==='object'){
此。移位日期(值);
}
}
}

您能在这里写下邮政服务吗?无论如何,这是一件好事。日期代表一个精确的时间点。它没有时区。UTC是表示该时间点的最佳方式。如果服务器需要知道时区,请将日期格式化为ISO格式,包括时区,或者将时区作为其他格式的一部分发送nt字段。@DrNio:我已经编写了一个后端服务来控制我的所有连接服务器。
public httpPostTypetxt(url:string,object:any,headers:HttpHeaders):可观察的{headers=headers.set('Content-Type','application/json');返回这个.http.post(this.configuration.serverwithapirl+url,object,{headers:headers,with credentials:true,注意:“response”,responseType:'text'}).map((response)=>{return this.parseResponse(response);}).catch((err)=>this.handleError(err,willBlock,loadingBar));}
您可以对其进行转换,并将其作为文件发送到服务器string@h.jalilzade在你的问题中发布相关代码。它在评论中无法阅读。而且你发布的代码没有任何日期。完美答案。这是我考虑从Angular迁移到Blazor的另一个原因。Angular中的日期太难了。我们需要ull在一个库中,例如矩,做最简单的事情。这个拦截器方法非常出色。到目前为止,使用它将身份验证令牌添加到我的请求中,并在令牌过期时重定向到登录。现在,这个!谢谢