Date object
Use new Date()
to create a new date object with the current date and time (that is, at the time that it is used - it is not automatically updated to stay current after that). You can store the date and time of your choice by specifying in the parentheses the year, month, day, plus optional hours, minutes, seconds, and milliseconds.
The function typeof
called on a date object will return the value object
.
The object has the following methodsA function associated with a class.. The hours range from 0 to 23, as they are given in 24-hour time.
.getFullYear()
.getMonth()
.getDate()
.getHours()
.getMinutes()
.getSeconds()
There are also methods which allow you to set particular parts of the date, such as .setMonth()
.
Caution!
The encoding is very strange, as months are numbered from 0 to 11 but days are numbered starting from 1.
When displayed in HTML, a date object is automatically converted to a string. The method .toDateString()
gives a more readable format.
Caution!
Once a variable has been set to a date object, it does not automatically update to the current time.