Sof*_*com 6 angular-material2 angular
我正在使用最新的Angular 4和Angular-Material2-2.0.0-beta7.这是我的模板使用MdDatePicker:
<md-input-container fxFlex="33" class="birthday-container">
<input mdInput name="birthday" placeholder="Birthday" required [(ngModel)]="member.birthday" [mdDatepicker]="birthdayPicker">
<button mdSuffix [mdDatepickerToggle]="birthdayPicker"></button>
</md-input-container>
<md-datepicker #birthdayPicker></md-datepicker>
Run Code Online (Sandbox Code Playgroud)
在app.module中,这是提供者:
{provide: DateAdapter, useClass: NativeDateAdapter}
Run Code Online (Sandbox Code Playgroud)
并且member.birthday是Date类型.但是,当JSON.stringify(member.birthday)它成为选定日期的前一天.例如:
选择2017-4-1从日期选择器,和字符串化的结果2017-03-31T13:00:00.000Z.
这篇文章提出了同样的问题,但我不确定如何申请moment.js代码.
当我们在日期选择器中选择日期时,所选内容将以 GMT(根据格林威治时区)时间存储和操作,但会以格式化表示形式显示给用户,同时考虑到用户当前的时区。因此存储和显示的日期是不同的(除了+0时区的用户,例如格林威治)。
当字符串化 JS Date 对象时,我们得到了明确的格林威治时区时间,我们可以在https://time.is/GMT看到它。您可能期望看到的是使用当前时区格式化的时间表示形式。请参阅我的示例中的差异。
// get current time
let date = new Date();
console.log('NATIVE JS DATE TIME', date.toString());
console.log('STRINGIFIED TIME', JSON.stringify(date));
console.log('FORMATTED TIME', `${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`);Run Code Online (Sandbox Code Playgroud)
您可以使用非常方便的时刻format()功能来显示日期。
| 归档时间: |
|
| 查看次数: |
5714 次 |
| 最近记录: |