简体   繁体   English

日期格式错误

[英]Date format coming wrong

vr_date :Date vr_date :日期

alert(this.vr_date ) // Result Shows Thu Feb 07 2019 00:00:00 GMT+0400 alert(this.vr_date ) // 结果显示 2019 年 2 月 7 日星期四 00:00:00 GMT+0400

var json = JSON.stringify(this.vr_date); var json = JSON.stringify(this.vr_date);

alert(json);警报(json); // Result Shows 2019-02-06T20:00:00.000Z see the date goes wrong // 结果显示 2019-02-06T20:00:00.000Z 看到日期出错

the output day shows 06 instead of 07输出日期显示 06 而不是 07

my html我的 HTML

<input matInput 
   [(ngModel)]="vr_date" 
   name="vr_date"  
   [matDatepicker]="myDatepicker" 
   matInput 
   placeholder="Vr Date" 
   [readonly]="true" >
<mat-datepicker-toggle matSuffix [for]="myDatepicker" ></mat-datepicker-toggle>
<mat-datepicker #myDatepicker></mat-datepicker>

Looks like the timezone is different.好像时区不一样。 In the first alert, you get day 07, time 00:00 with GMT +4.在第一个警报中,您将获得第 07 天,时间 00:00(格林威治标准时间 +4)。 In the second alert, you get the definition of the date object, (stored in GMT format).在第二个警报中,您将获得日期对象的定义(以 GMT 格式存储)。 Now, if you substract those +4 hours from the first alert, you get the second alert: day: 06, time 20:00.现在,如果您从第一个警报中减去 +4 小时,您将得到第二个警报:天:06,时间 20:00。

我喜欢使用 this.vr_date.setHours(this.vr_date.getHours() - this.vr_date.getTimezoneOffset() / 60);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM