new Date() / Date.now()
Creates a Date object for working with dates and times. You can get the current date and time, create a Date for a specific date, or retrieve a timestamp.
Syntax
// Creates a Date object with the current date and time.
var now = new Date();
// Creates a Date object from a date string.
var date = new Date("2025-04-15");
// Creates a Date object by specifying year, month, day, etc. individually. Month is zero-based.
var date = new Date(year, month, day, hours, minutes, seconds);
// Gets the current timestamp in milliseconds.
var timestamp = Date.now();
// Converts a date string to a timestamp.
var timestamp = Date.parse("2025-04-15");
Creation Methods
| Syntax | Description |
|---|---|
| new Date() | Called with no arguments, creates a Date object set to the current date and time. |
| new Date("date string") | Creates a Date from an ISO 8601 string. Examples include "2025-04-15" and "2025-04-15T10:30:00". |
| new Date(year, month, ...) | Specifies year, month, day, hours, minutes, seconds, and milliseconds individually. Month is zero-based: 0 is January and 11 is December. |
| new Date(timestamp) | Creates a Date from the number of milliseconds since January 1, 1970. |
| Date.now() | Returns the current timestamp in milliseconds. No Date object is created. |
| Date.parse("date string") | Converts a date string to a timestamp and returns it. Returns NaN for invalid strings. |
Sample Code
// Gets the current date and time.
var now = new Date();
console.log(now); // Outputs the date and time at the moment of execution.
// Creates a Date from a date string.
var date1 = new Date("2025-04-15");
console.log(date1); // Outputs: 'Tue Apr 15 2025 09:00:00 GMT+0900'
// Specifies year, month, and day individually. Month is zero-based, so 3 is April.
var date2 = new Date(2025, 3, 15, 10, 30, 0);
console.log(date2); // Outputs: 'Tue Apr 15 2025 10:30:00 GMT+0900'
// Gets the current timestamp using Date.now().
var timestamp = Date.now();
console.log(timestamp); // Outputs the milliseconds elapsed since January 1, 1970.
// Creates a Date object from a timestamp.
var fromTimestamp = new Date(1713150000000);
console.log(fromTimestamp); // Outputs the date and time corresponding to the timestamp.
// Converts a string to a timestamp using Date.parse().
var parsed = Date.parse("2025-04-15T10:30:00");
console.log(parsed); // Outputs the timestamp in milliseconds.
// Practical example: measuring execution time
var start = Date.now();
for (var i = 0; i < 1000000; i++) {} // Some processing
var end = Date.now();
console.log(end - start + "ms"); // Outputs the time the processing took.
Overview
The Date object is a built-in JavaScript object for working with dates and times. Internally, it stores date and time as the number of milliseconds elapsed since January 1, 1970, 00:00:00 UTC. This reference point is called the Unix epoch.
When creating a Date with new Date(year, month, day), note that the month is zero-based. January is 0 and December is 11. This is one of the most common mistakes in JavaScript. The day, however, starts at 1, so April 15 is written as new Date(2025, 3, 15).
Date.now() retrieves a timestamp without creating a Date object, making it efficient for scenarios where you only need a numeric timestamp, such as measuring execution time. To get individual components like year, month, or day from a Date object, use methods such as date.getFullYear().
Browser Compatibility
2 or earlier ×
2 or earlier ×
Android Browser
37+ ○
4 or earlier ×
Chrome Android
36+ ○
17 or earlier ×
Firefox Android
79+ ○
3 or earlier ×If you find any errors or copyright issues, please contact us.