JavaScript new Date()

Definition and usage

new Date() The constructor is used to create a new Date object.

Instance

There are five ways to instantiate (create) a new Date object:

Example 1

// Create a date object
const time = new Date();

Try it yourself

Example 2

Create a new date using ISO notation:

const time = new Date(dateString);

Try it yourself

Example 3

Create a new date from an existing date:

const time2 = new Date(time1);

Try it yourself

Example 4

Create a new date using the number of milliseconds since January 1, 1970:

const time = new Date(milliseconds);

Try it yourself

Example 5

Create a Date object with a specified date and time using new Date(7 numbers):

const time = new Date(year, month, day, hours, minutes, seconds, milliseconds);

Try it yourself

Syntax

new Date(date)

Parameter

Parameter Description
date Optional. Timestamp (in milliseconds) or date-time string.

Return value

Type Description
Date A new Date object.

Browser Support

new Date() Is an ECMAScript1 (JavaScript 1997) feature.

All Browsers Support:

Chrome IE Edge Firefox Safari Opera
Chrome IE Edge Firefox Safari Opera
Support Support Support Support Support Support

Related Pages

JavaScript Date

JavaScript Date Format

JavaScript Date Get Methods

JavaScript Date Set Methods