PrimeNG 日历 <p-calendar> - 如何禁用 UTC - Z 时区

PrimeNG Calendar <p-calendar> - How to disable UTC - Z timezone

有谁知道是否可以禁用 date/time 选择器自动将 UTC 时间添加到日期对象?正如您在下图中看到的那样,它会自动将我的日期对象调整为 UTC。我希望我的日期对象提交 10:00:00

{"reportedDate": "2019-02-13T15:00:16.000Z"}


<p-calendar required [(ngModel)]="entry.reportedDate" name="reportedDate" #reportedDate="ngModel" [showIcon]="true" [showTime]="true" dateFormat="mm/dd/y 'EST'" hourFormat="24"></p-calendar>

使用偏移量将日期格式化为您想要的时区。

*编辑 不好的例子(不要这样做):

var dt = new Date(1458619200000);
console.log(dt); // Gives Tue Mar 22 2016 09:30:00 GMT+0530 (IST)

dt.setTime(dt.getTime()+dt.getTimezoneOffset()*60*1000);
console.log(dt); // Gives Tue Mar 22 2016 04:00:00 GMT+0530 (IST)

var offset = -300; //Timezone offset for EST in minutes.
var estDate = new Date(dt.getTime() + offset*60*1000);
console.log(estDate); //Gives Mon Mar 21 2016 23:00:00 GMT+0530 (IST)

根据经验: 我不知道你是如何存储这个日期的。当您谈论时区时,事情很快就会变得复杂。稍后当您确实想要支持显示不同时区时。然后,您必须在客户端设备上从 Eastern 转到 Central。您数据库中的所有条目均采用东部时间,并且后端需要转换。您的服务器现在托管在不同的 TimeZone 中,您需要在每个服务调用中自定义逻辑(噩梦!!!)

现在帮自己一个忙,用 UTC 保存所有内容。

根据 the documentation page for this component(在属性 table 中):

Name        Type      Default    Description
-----------------------------------------------------------------------------
dataType    string    date       Type of the value to write back to ngModel,
                                 default is date and alternative is string.

你应该通过 [dataType]="string"。这将阻止构建 Date 对象,进而阻止任何时区转换。

另外,我建议不要在条目的格式中加入时区缩写。请记住,夏令时可能会生效,具体取决于所选日期,而且并非每个时区都有清晰一致的缩写,并且某些缩写(例如 CST 或 IST)不明确。

如果您需要向用户表明该条目采用的是美国东部时间,请将其放在文本框之外的某个位置。如果您必须使用缩写,请使用ET作为通用形式。