JavaScript Date object is used to represent a moment in time. This time value is since 1 January 1970 UTC (Coordinated Universal Time).
We can create a date using the Date object by calling the new Date() constructor as shown in the below syntax.
Syntax:
new Date();
new Date(value);
new Date(dateString);
new Date(year, month, day, hours, minutes, seconds, milliseconds);
Parameters: All of the parameters as shown in the above syntax are described below:
- value: This value is the number of milliseconds since January 1, 1970, 00:00:00 UTC.
- dateString: This represents a date format.
- year: This is represented by integer values ranging from the years 1900 to 1999.
- month: This is represented by integer values ranging from 0 for January to 11 for December.
- day: This is an optional parameter. This is represented by an integer value for the day of the month.
- hours: This is optional. This is represented by an integer value for the hour of the day.
- minutes: This is optional. This is represented by an integer value for the minute of a time.
- seconds: This is optional. This is represented by an integer value for the second of a time.
- milliseconds: This is optional. This is represented by an integer value for the millisecond of a time.
Return Values: It returns the present date and time if nothing as the parameter is given otherwise it returns the date format and time in which the parameter is given.
Let’s see JavaScript programs on the Date object.
Example 1: If nothing as the parameter is given, it returns the present date and time.
javascript
let A = new Date();
console.log(A);
|
Output
2023-08-01T05:55:27.313Z
Example 2: When an integer value is passed as the parameter then it gives the number of milliseconds since January 1, 1970, 00:00:00 UTC.
javascript
let A = new Date(32549);
console.log(A);
|
Output
1970-01-01T00:00:32.549Z
Example 3: When a date String is passed as the parameter then it returns the same as the parameter including the day name.
javascript
let A = new Date( 'June 22, 1985 07:19:35' );
console.log(A);
|
Output
1985-06-22T07:19:35.000Z
Example 4: When some numbers are passed as parameters then they are considered as year, month, day, hours, minutes, seconds, and milliseconds respectively.
javascript
let A = new Date(1996, 10, 13, 5, 30, 22);
console.log(A);
|
Output
1996-11-13T05:30:22.000Z
Errors and Exceptions: Open your console to check these errors.
Example 1: A valid integer variable should be passed as the parameter, otherwise it throws an error.
javascript
let A = new Date(gfg);
console.log(A);
|
Output:
Error: gfg is not defined
Example 2: It throws an error if a complex number is passed as a parameter.
javascript
let A = new Date(1 + 5i);
console.log(A);
|
Output:
Error: Invalid or unexpected token
Example 3: If an invalid date string is passed to the constructor, it throws an error.
javascript
let A = new Date( "geeksforgeeks" );
console.log(A);
|
Output:
Invalid Date
Application: It has many applications such as getting the exact current date and time. The below program prints the current date and time using the Date() object.
javascript
let A = new Date();
console.log(A);
|
Output
2023-08-01T05:57:13.267Z
We have a complete list of Javascript Date object methods, to check those please go through this JavaScript Date Object Complete Reference article.
Supported Browsers: The browsers supported by JavaScript Date are listed below:
- Google Chrome 1 and above
- Edge 12 and above
- Firefox 1 and above
- Opera 3 and above
- Safari 1 and above
Last Updated :
20 Nov, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...