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?
There are multiple ways to remove the local time zone from a date: Use the toUTCString() method to convert the date to a string using UTC. Use the toLocaleDateString() method to format the date according to a specific locale. Format the date and time consistently, e.g. as YYYY-MM-DD hh:mm:ss .
Yes, Date. now() will give you the same UTC timestamp independent of your current timezone. Such a timestamp, rather a point in time, does not depend on timezones. The Java equivalent new Date() gives you the exact same thing.
You can't change the Date object's behavior to get it to use a different time zone just by adding or subtracting an offset. It will still use the local time zone of where it runs, for any function that requires a local time, such as . toString() and others.
Definition and Usage. getTimezoneOffset() returns the difference between UTC time and local time. getTimezoneOffset() returns the difference in minutes. For example, if your time zone is GMT+2, -120 will be returned.
A Date object's underlying value is actually in UTC. To prove this, notice that if you type new Date(0)
you'll see something like: Wed Dec 31 1969 16:00:00 GMT-0800 (PST)
. 0 is treated as 0 in GMT, but .toString()
method shows the local time.
Big note, UTC stands for Universal time code. The current time right now in 2 different places is the same UTC, but the output can be formatted differently.
What we need here is some formatting
var _date = new Date(1270544790922); // outputs > "Tue Apr 06 2010 02:06:30 GMT-0700 (PDT)", for me _date.toLocaleString('fi-FI', { timeZone: 'Europe/Helsinki' }); // outputs > "6.4.2010 klo 12.06.30" _date.toLocaleString('en-US', { timeZone: 'Europe/Helsinki' }); // outputs > "4/6/2010, 12:06:30 PM"
This works but.... you can't really use any of the other date methods for your purposes since they describe the user's timezone. What you want is a date object that's related to the Helsinki timezone. Your options at this point are to use some 3rd party library (I recommend this), or hack-up the date object so you can use most of it's methods.
Option 1 - a 3rd party like moment-timezone
moment(1270544790922).tz('Europe/Helsinki').format('YYYY-MM-DD HH:mm:ss') // outputs > 2010-04-06 12:06:30 moment(1270544790922).tz('Europe/Helsinki').hour() // outputs > 12
This looks a lot more elegant than what we're about to do next.
Option 2 - Hack up the date object
var currentHelsinkiHoursOffset = 2; // sometimes it is 3 var date = new Date(1270544790922); var helsenkiOffset = currentHelsinkiHoursOffset*60*60000; var userOffset = _date.getTimezoneOffset()*60000; // [min*60000 = ms] var helsenkiTime = new Date(date.getTime()+ helsenkiOffset + userOffset); // Outputs > Tue Apr 06 2010 12:06:30 GMT-0700 (PDT)
It still thinks it's GMT-0700 (PDT), but if you don't stare too hard you may be able to mistake that for a date object that's useful for your purposes.
I conveniently skipped a part. You need to be able to define currentHelsinkiOffset
. If you can use date.getTimezoneOffset()
on the server side, or just use some if statements to describe when the time zone changes will occur, that should solve your problem.
Conclusion - I think especially for this purpose you should use a date library like moment-timezone.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With