Hadi Pawar
Hadi Pawar

Reputation: 1126

Managing timezones in moment js

I know this question is a bit generic but bear with me. I have an app which basically posts to website based on a user schedule. Now the app works great locally I m using moment js to compare the scheduled date/time with the current time and then if current time has exceeded scheduled time then that means it should start the auto posting process. The problem arises when I deploy the app to production the time there is different to the time here because of different timezone. How do I ensure (in moment js) that whatever time user schedules at whatever corner of the world the server posts it accordingly and does not start checking on what time it has there. To give you an example at the time of writing this it is Saturday 08/08/2020 00:48 here in PST, the server time is Friday 7/08/2020 19:47. So if I schedule here for say 1am, its only 10 mins to 1am here but for the server there is several hours. how do I manage that? Any ideas or pointing me in the right direction would be really helpful thank you for your time.

Upvotes: 0

Views: 932

Answers (2)

Miguel García
Miguel García

Reputation: 21

You could use moment.utc() on your datetime variable and then you would have 2 options. Make your system works based on utc timezone always or convert the utc datetime to any timezone you want using the moment timezone library.

https://momentjs.com/timezone/

Upvotes: 2

Kundan Kumar
Kundan Kumar

Reputation: 401

There could be multiple approaches to solve this problem statement. One approach is you can calculate the number of seconds from the created time(the current time) to post the content instead of the exact time at the browser from the chosen time by the user and send that to the server to store and run the logic accordingly at the server.

Upvotes: 1

Related Questions