![]() The zones are determined by the governments after accounting for their geography, and the official time of a country is maintained by the respective governing authorities. However, although the timezones are demarcated by the longitudes, each timezone is not always a straight line cutting through a country. As a result, the world is divided into 24 timezones shifted by one hour each. As Earth turns by 15° around its axis, time changes by one hour a 360º-degree rotation yields 24 hours. This is why the northeastern states have for a long time been demanding that they have a timezone distinct from the Indian Standard Time (IST), so they can take advantage of the early daylight hours.Ĭountries across the world keep different times because of Earth’s rotation and revolution around the Sun. On the other hand, if you travel further east to the eastern edge of Assam, light dawns as early as 4:30 am in summer and it gets dark by around 4 pm in winter. Getting the timezone name is a little more difficult, it's difficult to get it without other information.If you have ever travelled from Kolkata to Mumbai, you have likely noticed that daylight arrives later in the morning in Mumbai than in Kolkata, and sunlight lasts later into the evening. Thanks.Ĭonsole.log(d.toLocaleString(undefined, `) Instead, read my answer on How to initialize a JavaScript Date to a particular time zone. In general, you don't want to take this approach. This can only lead to errors, evident by the time zone shown in toString output, and will arise mathematically near any DST transitions of the local time zone and the intended target time zone. Here is another example of epoch shifting done correctly.īut in your example, you are shifting (twice in error) and then using the Date object as if it were normal and not shifted. Epoch shifting done right is what powers libraries like Moment.js. The catch is: once shifted, you can't ever use any of the local time functions on that Date object, or pass it to anything else that expects the Date object to be a normal one. ![]() The timestamp is adjusted to shift the base epoch away from the normal, thus allowing one to take advantage of the Date object's UTC functions (such as getUTCHours and others). There is only one scenario where this sort of adjustment makes sense, which is a technique known as "epoch shifting". It will still use the local time zone of where it runs, for any function that requires a local time, such as. 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. Passing it this california_ms timestamp is actually just picking a different point in time. When the Date constructor is passed a numeric timestamp, it must be in terms of UTC. Any function such as your getCaliforniatimezoneOffsetMS would need to have the timestamp passed in as a parameter to be effective.Īnd finally the date object let california_date: Date = new Date(california_ms) ![]() Also, unlike India, California observes daylight saving time, so part of the year the offset will be 480 (UTC-8), and part of the year the offset will be 420 (UTC-7). ![]() Now add california's timezone offset value let california_ms = utc_ms + getCaliforniaTimezoneOffsetMS()Īgain, adding an offset is incorrect. (also, the abbreviation is UTC, not UTS.) getTime() already returns a UTC based timestamp. The local time zone is applied when functions and properties that need local time are called - not when the Date object is created.Īdd it's timezone offset value let uts_ms = india_date.getTime() + india_date.getTimezoneOffset() Keep in mind that internally, the Date object only tracks a UTC based timestamp. If it is run on a computer with a different time zone, it will reflect that time zone instead. You have named this variable india_date, but the Date object will only reflect India if the code is run on a computer set to India's time zone. First, let's talk about the code in your question. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |