Search code snippets, questions, articles...

Get current timestamp in javascript

In this code post, we are going to describe different methods that can be used to get the timestamp using Javascript. We use the Javascript Date() function for that.
const timestamp = new Date().getTime();

console.log(timestamp);
Best JSON Validator, JSON Tree Viewer, JSON Beautifier at same place. Check how cool is the tool

Output

1643918032196

Live Demo

Use getTime() function to get the current timestamp

The getTime() function of the javascript Date() method returns the current timestamp. We can use this timestamp to verify when the specific task was performed. Below is an example.

# Code example 2

const result = new Date().getTime();

console.log(result);

In the above code snippet, we are using the 'new Date().getTime()' to get the current timestamp from a date.

Use plus '+' operator to get the timestamp in javascript

We can also use the plus '+' operator of Javascript to get the timestamp. You just need to place the + operator before the Date() function and it will return the timetstamp.

Syntax

+ new Date()

# Code example 3

const timestamp = + new Date();

console.log(timestamp);

Output

1647761974083

Try it yourself

const timestamp = new Date().valueOf();

console.log(timestamp);
The above code example shows how to get the current timestamp using the valueOf() function. It is a method of Javascript Date() function that returns the current timestamp.
Was this helpful?
0 Comments