Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore user's time zone and force Date() use specific time zone

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?

like image 749
warpech Avatar asked May 05 '10 08:05

warpech


People also ask

How do I remove the timezone from a date object?

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 .

Does date now () depend on timezone?

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.

How does JavaScript handle different time zones?

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.

What is timezone offset in JavaScript?

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.


1 Answers

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.

like image 142
Parris Avatar answered Sep 17 '22 14:09

Parris