How to Get the Timestamp in JavaScript?
Last Updated :
20 Nov, 2024
A timestamp is a numeric representation of the current time. It is a unique identifier that marks the exact moment when an event occurred or when a certain action was performed.
1. Using Date.now() Method
This approach uses Date.now() method. This method returns the number of milliseconds since January 1, 1970, 00:00:00 UTC. It is a static method of the Date object and is widely supported by modern browsers.
Syntax
let timestamp = Date.now();
Example: Timestamp
will hold the current timestamp in milliseconds, and it will be logged to the console. If you want the timestamp in seconds, you can divide the result by 1000:
JavaScript
const timestamp = Date.now();
console.log(timestamp);
2. Using new Date().getTime() Method
This approaches uses new Date().getTime() method. This method also returns the number of milliseconds since January 1, 1970, 00:00:00 UTC. It is an instance method of the Date object and is equivalent to calling the valueOf() method.
Syntax
let timestamp = new Date().getTime();
Example: The new Date().getTime()
method is used to create a new Date
object and retrieve its timestamp in milliseconds. The timestamp
variable will hold the current timestamp, and it will be logged to the console.
JavaScript
const timestamp = new Date().getTime();
console.log(timestamp);
3. Using new Date().valueOf() Method
This approach uses new Date().valueOf() method. This method returns the primitive value of a Date object, which is the number of milliseconds since January 1, 1970, 00:00:00 UTC.
Syntax
let timestamp = new Date().valueOf();
Example: The new Date().valueOf()
creates a new Date
object and retrieves its timestamp in milliseconds. The timestamp
variable holds this value, which is then logged to the console.
JavaScript
const timestamp = new Date().valueOf();
console.log(timestamp);
4. Using +new Date() Method
This approach leverages the unary plus operator (+) to implicitly convert the Date object into a number, effectively yielding the timestamp.
Example: This example shows the use of +new Date() method to get the timestamp.
JavaScript
let timestamp = +new Date();
console.log(timestamp);
Similar Reads
How to Get Current Time in JavaScript ? This article will show you how to get the current time in JavaScript. Whether you are creating a digital clock, scheduling events, or simply logging timestamps, knowing how to retrieve the current time is essential. Here, we will cover different methods to get the current time in JavaScript.Table of
2 min read
How to Get Milliseconds in JavaScript? In JavaScript, you can get the current time in milliseconds, which is useful for timestamping events, measuring time intervals, and performing time calculations. JavaScript provides various methods to precisely measure time down to the millisecond, including Date.now(), performance.now(), and the Da
2 min read
How to get the client timezone offset in JavaScript? The client's timezone offset could be detected by using the Date object's getTimezoneOffset() method. The getTimezoneOffset() method returns the time difference between UTC time and local time, that is the time offset, in minutes. This offset is changed by dividing by 60 and negating the result. Not
1 min read
How to get seconds since epoch in JavaScript? Given a date, we have to find the number of seconds since the epoch (i.e. 1 January 1970, 00:00:00 UTC ). The getTime() method in the JavaScript returns the number of milliseconds since January 1, 1970, or epoch. If we divide these milliseconds by 1000 and then integer part will give us the number o
1 min read
How to Get the Current Date in JavaScript ? The Date() object is used to access the current date in JavaScript. To get the current date in string format, we can use toDateString() method. The date.toDateString() method converts the given date object into the date portion into a string.SyntaxdateObj.toDateString()JavaScript// Create a Date Obj
2 min read