Skip to content
On this page

Date

In JavaScript, the Date object is used to represent dates and times.

To get the current system time, use:

javascript
let now = new Date();
now; // Wed Jun 24 2015 19:49:22 GMT+0800 (CST)
now.getFullYear(); // 2015, year
now.getMonth(); // 5, month (0~11, 5 means June)
now.getDate(); // 24, day of the month
now.getDay(); // 3, day of the week (Wednesday)
now.getHours(); // 19, in 24-hour format
now.getMinutes(); // 49, minutes
now.getSeconds(); // 22, seconds
now.getMilliseconds(); // 875, milliseconds
now.getTime(); // 1435146562875, timestamp in number format

Note that the current time is obtained from the local operating system, so it may not be accurate, as users can set their own date and time.

To create a Date object for a specific date and time, use:

javascript
let d = new Date(2015, 5, 19, 20, 15, 30, 123);
console.log(d); // Fri Jun 19 2015 20:15:30 GMT+0800 (CST)

A tricky point to observe is that JavaScript's month range is represented as integers from 0 to 11, where 0 is January and 1 is February, etc. Thus, to represent June, you pass in 5! This design choice can be frustrating, but fixing it now is impossible.

Important Note

Remember that JavaScript's Date object uses a month value that starts at 0: 0=January, 1=February, ..., 11=December.

Another way to create a Date object is by parsing a string that conforms to the ISO 8601 format:

javascript
let d = Date.parse('2015-06-24T19:49:22.875+08:00');
console.log(d); // 1435146562875

However, this returns a timestamp, not a Date object. You can easily convert this timestamp back to a Date:

javascript
let d = new Date(1435146562875);
d; // Wed Jun 24 2015 19:49:22 GMT+0800 (CST)
d.getMonth(); // 5

Important Note

When using Date.parse(), the input string should have actual month values from 01 to 12; however, after conversion, getMonth() will return values from 0 to 11.

Time Zone

The time represented by a Date object is always displayed in the browser's local time zone, but you can display both local and adjusted UTC times:

javascript
let d = new Date(1435146562875);
d.toLocaleString(); // '2015/6/24 下午7:49:22' (local time)
d.toUTCString(); // 'Wed, 24 Jun 2015 11:49:22 GMT' (UTC time)

In JavaScript, how do we handle time zone conversions? Actually, if we pass a timestamp of type number, we don't need to worry about time zone conversions. Any browser can accurately convert a timestamp to local time.

What is a timestamp? A timestamp is an incrementing integer that represents the number of milliseconds since January 1, 1970, at 00:00:00 GMT. If the computer's time is accurate, then the timestamp generated will be the same regardless of the time zone.

Thus, we just need to pass the timestamp or read it from a database, allowing JavaScript to automatically convert it to local time.

To get the current timestamp, you can use:

javascript
// Method 1:
console.log(Date.now());

// Method 2:
console.log(new Date().getTime());

Exercise

Xiaoming prepared a webpage to surprise his girlfriend on Valentine's Day, having reserved a French restaurant. He intended to use JavaScript to send her a surprise message:

javascript
let today = new Date();
if (today.getMonth() === 2 && today.getDate() === 14) {
    alert('Dear, I booked dinner at 6 PM at the restaurant!');
}

However, his girlfriend did not show up, leaving Xiaoming very disappointed. Can you analyze his JavaScript code to identify the problem?

Analysis: The problem lies in the condition today.getMonth() === 2. Since JavaScript months start at 0, February is represented as 1, not 2. Thus, the correct condition should be today.getMonth() === 1.

Date has loaded