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

て烟熏妆下的殇ゞ 提交于 2019-12-11 11:27:15

问题


Does anyone know if it's possible to disable the date/time picker from automatically adding the UTC time to the date object? As you can see in the below picture that it's automatically adjusting my date object to UTC. I want my date object submitted with 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>

回答1:


According to the documentation page for this component (in the properties table):

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

You should pass [dataType]="string". This will prevent a Date object from being constructed, which will in-turn prevent any time zone conversions.

Also, I recommend not putting a time zone abbreviation in the format for the entry. Keep in mind that daylight saving time might be in effect depending on the date chosen, and also that not every time zone has a clear and consistent abbreviation, and that some abbreviations (such as CST or IST) are ambiguous.

If you need to indicate to the user that the entry is in US Eastern Time, then put that somewhere outside of the textbox. If you must use an abbreviation, use ET as the generic form.




回答2:


Format the date to the timezone you want using the offset.

*edit BAD EXAMPLE (DON'T DO THIS):

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)

From Experience: I don't know how you're storing this date. Things can quickly become complicated when you're talking about time zones. Later on when you do want to support showing different time zones. You will then have to go from Eastern to Central on your clients device. All your entries in your DB are in Eastern Time and Conversions are needed in the backend. Your servers are now hosted in a different TimeZone and you need custom logic in every service call (Nightmares!!!)

Do yourself a favor now and save everything in UTC.



来源:https://stackoverflow.com/questions/54752165/primeng-calendar-p-calendar-how-to-disable-utc-z-timezone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!