Get the current timestamp in JavaScript
This post will discuss how to get the current timestamp in JavaScript. The solution should return the total number of milliseconds that elapsed between Unix Epoch and the current date.
Unix Epoch time is a way of representing a timestamp by representing the time as the number of seconds since 1 January 1970 00:00:00 UTC. To get the current timestamp in JavaScript, you can use one of the following functions:
1. Using Date.now() function
The recommended function is to use the static Date.now() function, which is a built-in function that returns the number of milliseconds since the Unix Epoch. Since now() is a static function, it doesn’t create a new instance of the Date object. This is demonstrated below:
|
1 2 |
let timestamp = Date.now(); console.log(timestamp); // e.g. 1656174169387 |
2. Using valueOf() or getTime() function
Another approach is to call the valueOf() or getTime() function, which returns the same value as Date.now(), but using an instance of the Date object. This function returns the primitive value of a Date object, which is the same as the UNIX timestamp.
|
1 2 3 4 5 |
let timestamp = new Date().valueOf(); console.log(timestamp); timestamp = new Date().getTime(); console.log(timestamp); // e.g. 1656164569387 |
If you use jQuery in your application, you can use the $.now() function, which is just a shorthand for the number returned by the expression (new Date).getTime().
|
1 2 3 4 5 6 7 |
// import jquery library const { JSDOM } = require("jsdom"); const { window } = new JSDOM(); let $ = require("jquery")(window); let timestamp = $.now(); console.log(timestamp); // e.g. 1656174569387 |
3. Using Number() constructor or Unary plus operator
A third way to get the current timestamp in JavaScript is using the Number() constructor, which is a built-in function that creates a number object from a given value. You can use it to convert a Date object to a number that represents the number of milliseconds since Unix Epoch.
|
1 2 3 4 5 6 |
const date = new Date(); // convert the Date object to a number const timestamp = Number(date); console.log(timestamp); // e.g. 1656174569487 |
Alternatively, you can use the expression +new Date(), which uses the unary plus operator (+) to converts a Date object into a Number. This is equivalent to calling Number(date), but shorter and less readable.
|
1 2 |
let timestamp = +new Date(); console.log(timestamp); // e.g. 1656174568387 |
4. Using Moment.js Library
The Moment.js library is a popular library that simplifies working with dates and times in JavaScript. The moment() function returns a moment object that wraps a Date object and provides many useful functions and properties. You can use the valueOf() or unix() function to get the timestamp in milliseconds or seconds, respectively, from the moment object.
|
1 2 3 4 5 6 7 8 |
// import Moment.js library let moment = require('moment'); let ms = moment().valueOf(); console.log(ms); // e.g. 1656114569387 let sec = moment().unix(); console.log(sec); // e.g. 1656114569 |
That’s all about getting the current timestamp in JavaScript.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)