setting timezone for timestamp in angular

712 Views Asked by At

Hello guys i looked the internet and here but i couldnt find the solutuon , anyway here is my problem.

I have a date and I need to convert this date to timestamp, but I need to do this according to canadian time. I am using the getTime() method, but this method converts to local time. As I change the clock of the pc, while my normal date remains constant, my timestamp date changes and this causes an error. Is there a method I can use instead of getTime() or how can I solve this problem? thanks for your solutions.

1

There are 1 best solutions below

3
Yousaf Raza On

The best approach is to store time in utc+0 offset and when displaying you can use date pipe to format it according to the required time zone.

Eg

The user is prompted to enter a date. Users enter the date according to his/her current timezone. say the timezone is UTC+5 and the time entered is yyyy-mm-dd:18:00:00, then its UTC equivalent would be yyyy-mm-dd:13:00:00 (actualTime - 5 hours)

The date is converted to UTC+0 using JS date object, see toUTCString

And when that date is displayed back in app convert it back to current timezone using angular datePipe. In this way time is synced is app is used in different timezones