Ade*_*gun 4 javascript momentjs typescript primeng-calendar
我正在 Angular 6 中开发。保存新记录时,我将表单值作为 json 保存在数据库中。如果最终用户想要显示存在记录,我会从 json 数据填充表单组件。但是我在转换日期值时遇到了麻烦。我无法正确投射我的本地日期。我已经尝试过,但没有奏效:
console.log("string Value",stringValue);
let date = moment(stringValue,"yyyy-mm-ddThh:mm:ss.fffZ");
console.log("date",date.format('DD/MM/YYY HH:mm:ss'));
Run Code Online (Sandbox Code Playgroud)
字符串值输出:2019-01-17T21:00:00.000Z
控制台输出实际:日期 18/01/2019 01:00:00
但预计控制台输出:日期 18/01/2019 00:00:00
我试过“YYYY-MM-DDThh:mm:ss.fffZ”但也没有用。
额外的信息
保存数据:
process.data = JSON.stringify(this.form.getRawValue());
save(process);
Run Code Online (Sandbox Code Playgroud)
html(primeng):
<p-calendar formControlName="startDate" dateFormat="dd.mm.yy"></p-calendar>
Run Code Online (Sandbox Code Playgroud)
You can parse your '2019-01-17T21:00:00.000Z' input using moment.utc() since it represents time in UTC
By default, moment parses and displays in local time.
If you want to parse or display a moment in UTC, you can use
moment.utc()instead ofmoment().
and then convert it to local timezone using local().
Here a live sample:
const stringValue = '2019-01-17T21:00:00.000Z';
let date = moment.utc(stringValue).local();
console.log("date", date.format('DD/MM/YYYY HH:mm:ss'));Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.23.0/moment.min.js"></script>Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12099 次 |
| 最近记录: |