JavaScript Dates
- Previous Page JS Array Const
- Next Page JS Date Format
JavaScript Date Output
By default, JavaScript will use the browser's time zone and display the date as a full text string:
Tue Apr 02 2019 09:01:19 GMT+0800 (China Standard Time)
Later, you will learn more about how to display dates in this tutorial.
Create Date object
The Date object is created by the new Date()
Constructor creation.
There are 4 methods to create a new date object:
- new Date()
- new Date(year, month, day, hours, minutes, seconds, milliseconds)
- new Date(milliseconds)
- new Date(date string)
new Date()
new Date()
Create a new date object with the current date and time:
Example
var d = new Date();
Date objects are static. The computer time is ticking, but the date object is not.
new Date(year, month, ...)
new Date(year, month, ...)
Create a new date object with specified date and time.
Seven digits specify year, month, day, hour, minute, second, and millisecond (in this order):
Example
var d = new Date(2018, 11, 24, 10, 33, 30, 0);
CommentJavaScript calculates months from 0 to 11.
January is 0. December is 11.
Six digits specify year, month, day, hour, minute, and second:
Example
var d = new Date(2018, 11, 24, 10, 33, 30);
Five digits specify year, month, day, hour, and minute:
Example
var d = new Date(2018, 11, 24, 10, 33);
Four digits specify year, month, day, and hour:
Example
var d = new Date(2018, 11, 24, 10)}
3 digits specify the year, month, and day:
Example
var d = new Date(2018, 11, 24);
2 digits specify the year and month:
Example
var d = new Date(2018, 11);
You cannot omit the month. If only one parameter is provided, it is considered milliseconds.
Example
var d = new Date(2018);
Last century
One and two-digit years will be interpreted as 19xx:
Example
var d = new Date(99, 11, 24);
Example
var d = new Date(9, 11, 24);
new Date(dateString)
new Date(dateString)
Create a new date object from a date string:
Example
var d = new Date("October 13, 2014 11:13:00");
Date strings will be introduced in the next chapter.
JavaScript stores dates as milliseconds
JavaScript stores dates as milliseconds since January 1, 1970, 00:00:00 UTC (Coordinated Universal Time).
Zero time is 00:00:00 UTC on January 1, 1970.
The current time is: 1,554,166,879,383 milliseconds after January 1, 1970.
new Date(milliseconds)
new Date(milliseconds)
Create a new date object with zero time plus milliseconds:
Example
var d = new Date(0);
Adding 100,000,000,000 milliseconds to January 1, 1970 is approximately March 3, 1973:
Example
var d = new Date(100000000000);
Subtracting 100,000,000,000 milliseconds from January 1, 1970 is approximately October 31, 1966:
Example
var d = new Date(-100000000000);
Example
var d = new Date(86400000);
One day (24 hours) is 86,400,000 milliseconds.
date methods
When creating a Date object, manyMethodsOperate on it.
Date methods allow you to get and set the year, month, day, hour, minute, second, and millisecond of a date object using local time or UTC (Coordinated Universal Time or GMT).
Date methods and time zones will be introduced in the next chapter.
Display the date
JavaScript (by default) outputs dates in full text string format:
Wed Mar 25 2015 08:00:00 GMT+0800 (China Standard Time)
When displaying a date object in HTML, the toString() method is automatically converted to a string.
Example
d = new Date(); document.getElementById("demo").innerHTML = d;
Equivalent to:
d = new Date(); document.getElementById("demo").innerHTML = d.toString();
toUTCString()
The method converts the date to a UTC string (a date display standard).
Example
var d = new Date(); document.getElementById("demo").innerHTML = d.toUTCString();
toDateString()
The method converts the date to a more readable format:
Example
var d = new Date(); document.getElementById("demo").innerHTML = d.toDateString();
- Previous Page JS Array Const
- Next Page JS Date Format