Detect timezone in javascript
For now I'm working on a project, I need to display the value of date on screen. The value I received , I get it as datetime in UTC value. I need to display as local timezone.
var time_value = 1391187600000;
I check the current timezone by use this method getTimezoneOffset
It is a method of object date in javascript, it return number of minutes different from UTC.
This is what I use
var hplus = ((new Date().getTimezoneOffset()) / 60) * -1);
if (isNaN(hplus)) hplus = 0;
explanation:
new Date()
- Need to use method of object date, then create one
new Date().getTimezoneOffset()
- Receive -420 (minutes) from this method
(new Date().getTimezoneOffset()) / 60
- Need hour so I divided it by 60, in my sample I will get 7
(((new Date().getTimezoneOffset()) / 60) * -1)
- As I received UTC, to display I will add 7 to UTC hour, I prefer to get +7
- If you are at Arizona in USA, you will received -7
No comments:
Post a Comment