1

Consider I'm from India and my Country ISO code is IN or IND or 356 (http://en.wikipedia.org/wiki/ISO_3166-1). How could I generate a JavaScript Date object with that? That is, in IST.

In Python, we have astimezone(). Anything similar to that is available in JavaScript?

PS: jQuery or any trusted third party API is allowed.

K DawG
  • 13,287
  • 9
  • 35
  • 66
Babu
  • 2,548
  • 3
  • 30
  • 47
  • See also the questions: [here](http://stackoverflow.com/q/15141762/634824), [here](http://stackoverflow.com/q/19695439/634824), and the [timezone tag wiki](http://stackoverflow.com/tags/timezone/info). Also realize that *many* countries have more than one time zone, so you cannot simply resolve it by country code. – Matt Johnson-Pint Dec 19 '13 at 15:46

3 Answers3

2

JavaScript in the browser is not a timezone aware language. It has the ability to work with only two timezones: UTC and local timezone where local timezone depends on the timezone of the Browser or the Operating System that the Browser runs on.

If your browser (and your user's browser) is set to IST, then all dates will display in IST. If your browser is not set to IST, then you're sort of out of luck.

Now, you can do some tricks. For example, you can do the following:

var d = new Date();  // creates a date in user's local timezone

var istD = new Date(d.getTime() + (d.getTimezoneOffset() + 330) * 60000)
// 330 is the IST offset (5h 30m == 330m)

istD is now a date object that will "print the date in IST". Note that this date object is still in the user's local timezone, but as long as you don't display the timezone part, it will appear to be in IST.

There is a small problem with Daylight Saving Time. Since IST does not have DST, this problem is minimized, but still exists if the user's local timezone has DST and you are just a few hours off from the DST offset. You might be able to play around with the Date.getUTC* functions to circumvent the DST issues, however always remember that there is no way to correctly do timezones in clientside JavaScript. There is no library in existence today that correctly handles all TimeZone rules, including Daylight Savings rules for all historical dates.

bluesmoon
  • 3,918
  • 3
  • 25
  • 30
  • That explains a lot! What is that `330`? Is it something related to IST? What if I want the time other than IST? – Babu Dec 19 '13 at 05:44
  • 1
    updated the answer. 330 is the IST offset in minutes (5hrs 30 minutes). If you want a different timezone, use that timezone's offset instead, and keep in mind that the offset is different for different date/times depending on DST rules. – bluesmoon Dec 19 '13 at 06:05
  • @bluesmoon - the part about no library existing is false. I list five of them [here](http://stackoverflow.com/a/15171030/634824). They all work to various degrees, but there is no clear "winner" at the moment. – Matt Johnson-Pint Dec 19 '13 at 15:50
  • @MattJohnson thanks, that's pretty cool. The test I use is fairly simple. See if a library can correctly display a full date range starting in Standard time, and ending in Daylight saving time across multiple years when a government decided to change DST rules (eg: 2007 in the US) – bluesmoon Dec 20 '13 at 17:17
  • 1
    Yep, they all can do that, because they use the IANA time zone data. They can all do UTC to local conversion, most (bt not all) can do the opposite also. Some (like timezone.js) have bugs specifically right at the transition points, but work fine in general. Eventually, one will probably shine through as the obvious choice. For now, experiment. :) – Matt Johnson-Pint Dec 20 '13 at 17:23
1

You can set the date with UTC and then you can convert it into LocaleString. You can see Date.prototype.toLocaleString() for more refrence.

And you can also see the question asked on stack overflowHow do I display a date/time in the user's locale format and time offset?.

Thanks

Community
  • 1
  • 1
0

There is no directly API.But you can coding it by your self.


<html>
<body onload="getTimeByTimeZone(5.5)">

<script type="text/javascript">
    function getTimeByTimeZone(offset){
        var d = new Date()
        localTime = d.getTime();
        localOffset=d.getTimezoneOffset()*60000;
        utcTime=localTime + localOffset;//get utc time
        st=utcTime+3600000*offset;//get the specified timezone time
        stime=new Date(st);
        document.write("The specified time is :"+stime.toString());
    }

</script>
</body>
</html>

Hope this code is helpful to you.

dalei19
  • 121
  • 1
  • 1
  • 6