Get the current timestamp from a specific time zone

Is there an easy way to get unix timestamp in javascript from a specific timezone? for example, I want the client to send me a unix timestamp, but I want it to match my timezone.

thanks!

+6
source share
3 answers

Why not just send the date in UTC and then convert to your timezone on the server?

var utcEpochSeconds = dateObj.getTime() + (dateObj.getTimezoneOffset() * 60000); 
+5
source

For this to happen, you need to apply the time zone offset to time, and then remove the offset from the value (check this, I guess from memory):

 var now = new Date(), offset = -(now.getTimezoneOffset() * 60 * 1000), // now in milliseconds userUnixStamp = +now + offset; 

Now an offset from your own:

 var now = new Date(), offset = now.getTimezoneOffset() * 60 * 1000, yourUnixStamp = userUnixStamp - offset; 
+1
source

Use toISOString to get the UTC timestamp.

 var date = new Date(); date.toISOString(); // EST would be 6 hour diff from GMT 
0
source

Source: https://habr.com/ru/post/910045/


All Articles