In this article, you’ll learn how to get the current timestamp in javascript. The UNIX timestamp is an integer that represents the number of seconds elapsed since January 1, 1970. You find all functions for working with date and time in Javascript are in module Date. This module provides multiple ways how you can get the current timestamp in JavaScript.
Here are some examples to get the current Timestamp in Javascript.
When you create an object of Date without any parameter, then it uses current time, and it returns the timestamp in milliseconds. Let’s take an example:
var timeStamp = (new Date()).getTime();
console.log(timeStamp)
// Output(in milliseconds) ==> 1644321718881
In this example, you can use the Date.Now()
function. The Object Date
also provides a function now
()
which also returns the number of milliseconds elapsed since the beginning of the epoch(January 1, 1970). Let’s take an example:
var timeStamp = Date.now();
console.log(timeStamp)
// Output(in milliseconds) ==> 1644323353482
In this example, you can use the + new Date()
to get the current timestamp in javascript. It returns the timestamp in milliseconds. Let’s take an example:
var timeStamp = (+ new Date());
console.log(timeStamp)
// Output(in milliseconds) ==> 1644323353482
In this example, you can use the new Date().valueOf()
function to get the current timestamp in javascript. It returns the timestamp in milliseconds. Let’s take an example:
var timeStamp = new Date().valueOf();
console.log(timeStamp)
// Output(in milliseconds) ==> 1644323353482
I hope this article will help you to understand how to get the current timestamp in javascript.
Share your valuable feedback, please post your comment at the bottom of this article. Thank you!
Comments