0

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 Answers1

0

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

Yousaf Raza
  • 703
  • 3
  • 11
  • okey i understood but the logic is like i send timestamp to backend and backend converts timestamp to canada date and backend returns startDay so if i send wrong timestamp , backend returns wrong day so this is not about datePipe :( – atacan erdoğan Sep 23 '22 at 06:54
  • can you provide a minimal reproduction? – Yousaf Raza Sep 23 '22 at 07:42
  • here is my code startDay.getTime() this makes local timestamp startDay is seems like Wed Oct 12 2022 00:00:00 GMT+0300 (GMT+03:00) and timestamp is 1665522000000 i want to set timestamp in canada timezone – atacan erdoğan Sep 23 '22 at 12:41