7

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!

Toby
  • 2,720
  • 5
  • 29
  • 46

3 Answers3

4

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

var utcEpochSeconds = dateObj.getTime() + (dateObj.getTimezoneOffset() * 60000);
tjdett
  • 1,703
  • 1
  • 18
  • 23
  • By far the best solution since the local machine doesn't necessarily know the timezone offset of the server. Still can't work out why there isn't a `dateObj.getUTCTime()` method. – RobG Mar 06 '12 at 02:43
  • UTC is always the same, no matter what? (i suck at timezone stuff) – Toby Mar 07 '12 at 01:32
  • 1
    Yes, unless your clock is misconfigured, or you're travelling at relativistic speeds: http://en.wikipedia.org/wiki/Coordinated_Universal_Time – tjdett Mar 07 '12 at 03:04
  • but if i compare my local result with the UTC time im 8 hours off?! (im in LA timezone) – Toby Mar 07 '12 at 18:13
  • Well, yes, because you're in the US [Pacific Time Zone](http://en.wikipedia.org/wiki/Pacific_Standard_Time), which is eight hours behind UTC. Assuming your system clock and timezone are configured correctly, your UTC result should match the UTC result of everywhere else in the world with only a few seconds of drift. – tjdett Mar 07 '12 at 23:22
1

Use toISOString to get a UTC timestamp.

var date = new Date();
date.toISOString(); // EST would be 6 hour diff from GMT
Trevor
  • 11,269
  • 2
  • 33
  • 40
  • `toISOString()` is ES5 and not yet supported by a good percentage of web browsers in use. Not hard to emulate for those that lack it though. – RobG Mar 06 '12 at 02:47
  • Isn't it just old IE that doesn't support it? :P Either way, create a shim. – Trevor Mar 06 '12 at 14:49
1

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;
Eli
  • 17,397
  • 4
  • 36
  • 49
  • could you wrap that into an example where the client no matter what returns the same timestamp for los angeles? cause i cant wrap my mind around it right now... i keep getting different results. *brainmelt – Toby Mar 07 '12 at 01:31
  • Could you explain the situation a little more? Where is the date coming from? How are you getting to it? – Eli Mar 07 '12 at 01:51
  • i need to keep any timezone client at the same time of the server - i already set the server time to UTC just to make it easier - now i tried to generate the UTC time with this: var d = new Date(); var utc = Math.round( d.getTime() + ( d.getTimezoneOffset() * 60000 ) / 1000 ); but it always returns a microtime (why?! there's a /1000) timestamp that is 8 hours ahead... – Toby Mar 07 '12 at 01:54