By default, a Date
object is created as local time. This is not always desirable, for example when communicating a date between a server and a client that do not reside in the same timezone. In this scenario, one doesn’t want to worry about timezones at all until the date needs to be displayed in local time, if that is even required at all.
In this problem we want to communicate a specific date (day, month, year) with someone in a different timezone. The first implementation naively uses local times, which results in wrong results. The second implementation uses UTC dates to avoid timezones where they are not needed.
function formatDate(dayOfWeek, day, month, year) {
var daysOfWeek = ["Sun","Mon","Tue","Wed","Thu","Fri","Sat"];
var months = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
return daysOfWeek[dayOfWeek] + " " + months[month] + " " + day + " " + year;
}
// Foo lives in a country with timezone GMT + 1
var birthday = new Date(2000,0,1);
console.log("Foo was born on: " + formatDate(birthday.getDay(), birthday.getDate(),
birthday.getMonth(), birthday.getFullYear()));
sendToBar(birthday.getTime());
Sample output: Foo was born on: Sat Jan 1 2000
// Meanwhile somewhere else...
// Bar lives in a country with timezone GMT - 1
var birthday = new Date(receiveFromFoo());
console.log("Foo was born on: " + formatDate(birthday.getDay(), birthday.getDate(),
birthday.getMonth(), birthday.getFullYear()));
Sample output: Foo was born on: Fri Dec 31 1999
And thus, Bar would always believe Foo was born on the last day of 1999.
function formatDate(dayOfWeek, day, month, year) {
var daysOfWeek = ["Sun","Mon","Tue","Wed","Thu","Fri","Sat"];
var months = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
return daysOfWeek[dayOfWeek] + " " + months[month] + " " + day + " " + year;
}
// Foo lives in a country with timezone GMT + 1
var birthday = new Date(Date.UTC(2000,0,1));
console.log("Foo was born on: " + formatDate(birthday.getUTCDay(), birthday.getUTCDate(),
birthday.getUTCMonth(), birthday.getUTCFullYear()));
sendToBar(birthday.getTime());
Sample output: Foo was born on: Sat Jan 1 2000
// Meanwhile somewhere else...
// Bar lives in a country with timezone GMT - 1
var birthday = new Date(receiveFromFoo());
console.log("Foo was born on: " + formatDate(birthday.getUTCDay(), birthday.getUTCDate(),
birthday.getUTCMonth(), birthday.getUTCFullYear()));
Sample output: Foo was born on: Sat Jan 1 2000
If one wants to create a Date
object based on UTC or GMT, the Date.UTC(...)
method can be used. It uses the same arguments as the longest Date
constructor. This method will return a number representing the time that has passed since January 1, 1970, 00:00:00 UTC.
console.log(Date.UTC(2000,0,31,12));
Sample output: 949320000000
var utcDate = new Date(Date.UTC(2000,0,31,12));
console.log(utcDate);
Sample output: Mon Jan 31 2000 13:00:00 GMT+0100 (West-Europa (standaardtijd))