JavaScript | Date.now()

The Date.now() is an inbuilt function in JavaScript which returns the number of milliseconds elapsed since January 1, 1970, 00:00:00 UTC. Since now() is a static method of Date, it will always be used as Date.now().
Syntax:

var A = Date.now();

Where A is time in millisecond.
Parameters: This function accepts no parameter.
Return Values: It returns the number of milliseconds elapsed since January 1, 1970, 00:00:00 UTC.
JavaScript code to show the working of Date.now() function:
Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
  
  // Use of function Date.now()
  var A = Date.now();
  
  // Printing the number of millisecond elapsed 
  document.write("The time elapsed in millisecond is: " + A);
  
</script>    

chevron_right


Output:



The time elapsed in millisecond is: 1529644667834

Example 2:
To get the current date, use the code below.

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
  
  // Use of Date.now() function
  var d = Date(Date.now());
  
  // Converting the number of millisecond in date string
  a = d.toString()
  
  // Printing the current date                    
  document.write("The current date is: " + a) 
  
</script>

chevron_right


Output:

The current date is: Fri Jun 22 2018 10:54:33 GMT+0530 (India Standard Time)

Example 3:
The Date(Date.now()) is same as Date(), so the same result can be achieved i.e, current date using the following code.

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
  
  // Using Date() function
  var d = Date();
    
  // Converting the number value to string
  a = d.toString() 
    
  // Printing the current date
  document.write("The current date is: " + a)
    
</script>

chevron_right


Output:

The current date is: Fri Jun 22 2018 11:02:01 GMT+0530 (India Standard Time)

Supported Browsers: The browsers supported by JavaScript Date.now() Function are listed below:

  • Google Chrome
  • Internet Explorer 9.0
  • Firefox
  • Opera
  • Safari


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.