javascript
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);
Output
1643918032196
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
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?
Similar Posts
- Lodash - Get current timestamp in milliseconds
- Get the current month name using javascript
- Get current year using Javascript Date getFullYear() method
- Remove all cookies from the current website using Javascript
- Get Pseudo elements using Javascript
- Get all child elements of parent element in javascript
- Get first 10 words from string javascript