views:

271

answers:

2

In an JS app, I receive timestamp (eq. 1270544790922) from server (Ajax).

Basing on that timestamp I create Date object using:

var _date = new Date();
_date.setTime(1270544790922);

Now, _date decoded timestamp in current user locale time zone. I don't want that.

I would like _date to convert this timestamp to current time in city of Helsinki in Europe (disregarding current time zone of the user).

How can I do that?

+1  A: 

So once a date object is created in JS, it will convert to users local time. It is also possible to create a GMT version of that timestamp using. I think the first step is to attempt to confirm that the time being presented is in fact the users time and not GMT. Try using.

_date.getTimezoneOffset();

Once that is determined. You can add that time zone offset along with the offset for Helsenki which is +2. I am assuming that is actually the users local time as you mentioned. Try:

var _helsenkiOffset = 2*60*60;//maybe 3
var _userOffset = _date.getTimezoneOffset()*60*60; 
var _helsenkiTime = new Date(_date.getTime()+_helsenkiOffset+_userOffset);

That might be a little closer... So essentially we are just pushing 2 hours forward and accounting for gmt offset. The only thing in question is DST; however, if the server is giving you a time stamp it might already be DST. Otherwise, try something like this: http://www.csgnetwork.com/timezoneproginfo.html and in an if statement add or subtract 1 hour.

Parris
also... a similar task can be done, by simply using the gmt offset from one location. You don't need javascript at all in that case.
Parris
sorry but no, I mean the exact opposite :) I edited the question, maybe it is clearer now
warpech
Ok I changed my solution. I think this is what you are looking for.
Parris
Unfortunately that's the only thing that I also came up with. I thought that maybe browser could generate "_helsinkiOffset" for me.
warpech