Tutorialspoint.dev

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:

Code #1:

<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>    

Output:

The time elapsed in millisecond is: 1529644667834

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

<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>

Output:

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

Code #3:
The Date(Date.now()) is same as Date(), so the same result can be achieved i.e, current date using the following 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>

Output:

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


This article is attributed to GeeksforGeeks.org

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter