Friday, January 15, 2016

NTS: JavaScript time and timezones... [1]

Coordinated Universal Time (UTC), is the primary time standard by which the world regulates clocks and time. It is, within about 1 second, mean solar time at 0° longitude;
UTC is one of several closely related successors to Greenwich Mean Time (GMT).
Greenwich Mean Time (GMT) is the mean solar time at the Royal Observatory in Greenwich, London.
UTC, while based on zero degrees longitude, which passes through the Greenwich Observatory, is based on atomic time and includes leap seconds as they are added to our clock every so often. UTC was used beginning in the mid-twentieth century but became the official standard of world time on January 1, 1972.

Although GMT and UTC share the same current time in practice, GMT is a time zone officially used in some European and African countries. UTC is not a time zone, but a time standard that is the basis for civil time and time zones worldwide. This means that no country or territory officially uses UTC as a local time.

Neither UTC nor GMT ever change for Daylight Saving Time (DST). However, some of the countries that use GMT switch to different time zones during their DST period.
The Eastern Time Zone (ET) is a time zone encompassing 17 U.S. states in the eastern part of the contiguous United States, parts of eastern Canada, the state of Quintana Roo in Mexico, Panama in Central America and the Caribbean Islands.
  • Eastern Standard Time (EST) corresponds to ET standard time (autumn/winter) and it is 5 hours behind Coordinated Universal Time (UTC−05:00).
  • Eastern Daylight Time (EDT), is used by some places when observing daylight saving time (spring/summer) is 4 hours behind Coordinated Universal Time (UTC−04:00).

Creating The Date object

There are four ways to create the Date object:

new Date()

We can create a JavaScript Date 'now' object as follows:
var d = new Date();
The Date is created with timezone set to the Local Time timezone (EST for me):
//  d -> Tue Jan 12 2016 23:34:03 GMT-0500 (EST)
It is then possible to compute the timezone difference between UTC and Local Time (EST):
var n = d.getTimezoneOffset();
//  n -> 300 (minutes, equivalent to 5 hours)

new Date(milliseconds)

JavaScript dates are calculated in milliseconds from 01 January, 1970 00:00:00 Universal Time (UTC).
var d = new Date(0); 
//  d -> Wed Dec 31 1969 19:00:00 GMT-0500 (EST) -> D1
One day contains 86,400,000 milliseconds.
var d = new Date(86400000); 
//  d -> Thu Jan 01 1970 19:00:00 GMT-0500 (EST)
By providing a negative amount of milliseconds we will refer to a date prior to 01 January, 1970 00:00:00 Universal Time (UTC).
var d = new Date(-86400000*365); 
//  d -> Tue Dec 31 1968 19:00:00 GMT-0500 (EST) -> One year prior to D1

new Date(dateString)

var d = new Date("October 12, 1973 11:13:00")); 
//  d -> Fri Oct 12 1973 11:13:00 GMT-0400 (EDT)

new Date(year, month, day, hours, minutes, seconds, milliseconds)

Note that the months are identified by 0-11 numbers. Where 0 is January.
var d = new Date(73,9,12,11,13,00,0);
//  d -> Fri Oct 12 1973 11:13:00 GMT-0400 (EDT)
Also note that when the year is specified with an integer between 0-99, that will be translated to a year between 1900 and 1999. In other words when the year is identified by the last or the last two digits (0 = 1900).
var d = new Date(0,9,12,11,13,00,0);
//  d -> Fri Oct 12 1900 11:13:00 GMT-0400 (EDT)
However, the year can be identified by four digits:
var d = new Date(1899,9,12,11,13,00,0);
//  d -> Thu Oct 12 1899 11:13:00 GMT-0400 (EDT)
So, in order to define a date with year between 0 and 99 we can use the method setFullYear():
var d = new Date(19,9,12,11,13,00,0);
d.setFullYear(19);
//  d -> Sat Oct 12 19 11:13:00 GMT-0400 (EDT)
//  d -> -61543010820000 (ms from 01 January, 1970 00:00:00 UTC)
Note that the method setYear() will behave exactly as the constructor:
var d = new Date(19,9,12,11,13,00,0);
d.setYear(19);
//  d -> Sun Oct 12 1919 11:13:00 GMT-0400 (EDT)
//  d -> -1584866820000 (ms from 01 January, 1970 00:00:00 UTC)
Read more on the Mozilla Developer Network.

No comments: