Back to: JavaScript
In JavaScript the Date object represents a moment in time. The Date object contains a number that represents the number of milliseconds since 1 January 1970 UTC.
The maximum dates are ±8,640,000,000,000,000 since 1 January 1970 UTC, which is April 20, 217821 BCE – September 13, 275760 CE.
toTimeString()
toTimeString is a method that returns the time, in the local timezone:
const event = new Date('August 30, 2012 22:10:20');
console.log(event.toTimeString());
"22:10:20 GMT+0100 (British Summer Time)"
getDate()
getDate() is a method that returns the date, in the local timezone:
const event = new Date('August 30, 2012 22:10:20');
console.log(event.getDate());
"30"
getDay()
getDay() is a method that returns the day, in the local timezone:
const event = new Date('August 30, 2012 22:10:20');
console.log(event.getDay());
"4"
You maybe thinking that something is wrong, 4 is not a day of the week! This is however, correct. A number is returned, that corresponds to the day of the week. It begins with Sunday, which is 0. Therefore, 4 is equal to Thursday.
getHours()
getHours() is a method that returns the hour, in the local timezone:
const event = new Date('August 30, 2012 22:10:20');
console.log(event.getHours());
"22"
The result is in 24hr, therefore the range of possible values is 0 – 23.