How to Generate a Timestamp in JavaScript

  49 views   1 month ago Javascript

Use the Date.now() Method

You can simply use the JavaScript Date.now() method to generate the UTC timestamp in milliseconds (which is the number of milliseconds since midnight Jan 1, 1970).

The following example demonstrates how to get a timestamp and how to convert it back to the human-readable date and time format in JavaScript.

<script>
// Creating a timestamp
var timestamp = Date.now();
console.log(timestamp);
 
// Converting it back to human-readable date and time
var d = new Date(timestamp);
console.log(d);
</script>

 

Author : Harsukh Makwana
Harsukh Makwana

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on harsukh21@gmail.com

Related Articles