Pregunta

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

thanks!

¿Fue útil?

Solución

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

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

Otros consejos

Use toISOString to get a UTC timestamp.

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

In order for this to happen, you need to apply the timezone offset to the time, and then remove your offset from value (test this, I am guessing from memory):

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

Now offset from your own:

var now = new Date(),
    offset = now.getTimezoneOffset() * 60 * 1000,
    yourUnixStamp = userUnixStamp - offset;
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top