JavaScript | Date - GeeksforGeeks

12 Oct, 2021
The Date object in JavaScript is used to represent a moment of 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 which ranging from years 1900 to 1999.
month : This is represented by integer values which ranging from 0 for January to 11 for December.
day : This is an optional parameter. This is represented by integer value for the day of the month.
hours : This is optional. This is represented by integer value for the hour of the day.
minutes : This is optional. This is represented by integer value for the minute of a time.
seconds : This is optional. This is represented by integer value for the second of a time.
milliseconds : This is optional. This is represented by 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 return the date format and time in which parameter is given.Let’s see JavaScript programs on Date object.  
Example 1: If nothing as the parameter is given, it returns present date and time.  
javascript
<script>  // If nothing as parameter is given,  // it represent the present date and time.  var A = new Date();   // Printing present date and time.  document.write(A);</script>
Output:  
Wed Mar 21 2018 20:44:40 GMT+0530 (India Standard Time)
Example 2: When any integer value is taken as the parameter then it given the number of milliseconds since January 1, 1970, 00:00:00 UTC.  
javascript
<script>  // Parameter as integer value give the number of  // milliseconds since January 1, 1970, 00:00:00 UTC.  var A = new Date(32549);   document.write(A);</script>
Output:  
Thu Jan 01 1970 05:30:32 GMT+0530 (India Standard Time)
Example 3: When any dataString is given as the parameter then it return the same as the parameter including day name.  
javascript
<script>  // When any dataString is given as the parameter  // then it return the same as the parameter  // including day name.  var A = new Date('June 22, 1985 07:19:35');   document.write(A);</script>
Output:  
Sat Jun 22 1985 07:19:35 GMT+0530 (India Standard Time)
Example 4: When some numbers are taken as the parameter then they are considered as year, month, day, hours, minutes, seconds, milliseconds respectively.  
javascript
<script>  // When some numbers are taken as the parameter  // then they are considered as year, month, day,  // hours, minutes, seconds, milliseconds  // respectively.  var A = new Date(1996, 10, 13, 5, 30, 22);   document.write(A);</script>
Output:  
Wed Nov 13 1996 05:30:22 GMT+0530 (India Standard Time)
Errors and Exceptions: To check this you have to check in console.  
Example 1: Any integer number should be taken as the parameter not any name otherwise it gives error. 
javascript
<script>  // Any integer number should be taken  // as the parameter not any name.  var A = new Date(gfg);   document.write(A);</script>
Output:  
Error: gfg is not defined
example 2: Any integer number should be take as the parameter not any other number e.g- complex number.  
javascript
<script>  // Any integer number should be take as  // the parameter not any other number  // e.g- complex number.  var A = new Date(1 + 5i);   document.write(A);</script>
Output:  
Error: Invalid or unexpected token
Example 3: Any integer number should be take as the parameter not any other number e.g- complex number.  
javascript
<script>  // Any integer number should be taken  //  as the dateString not word.  var A = new Date("geeksforgeeks");   document.write(A);</script>
Output:  
Invalid Date
Application: It has many application such as for getting the exact current date and time. Below program prints the current date and time using the Date() object.  
javascript
<script>  // If nothing as parameter is given, it  // represent the present date and time.  var A = new Date();   // Printing present date and time.  document.write(A);</script>
Output:  
Wed Mar 21 2018 20:44:40 GMT+0530 (India Standard Time)
Supported Browsers: The browsers supported by JavaScript Date are listed below:  
Google Chrome 1 and above
Edge 12 and above
Firefox 1 and above
Internet Explorer 3 and above
Opera 3 and above
Safari 1 and above
 
ysachin2314
javascript-date
javascript-functions
JavaScript
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Difference between var, let and const keywords in JavaScript
Convert a string to an integer in JavaScript
Differences between Functional Components and Class Components in React
How to calculate the number of days between two dates in javascript?
File uploading in React.js
How to append HTML code to a div using JavaScript ?
How to Open URL in New Tab using JavaScript ?
Hide or show elements in HTML using display property
JavaScript | console.log() with Examples
How to Use the JavaScript Fetch API to Get Data?