;

How to Get the Current Timestamp in JavaScript


Tutorialsrack 08/02/2022 Jquery Javascript

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.

Example 1: Using Constructor

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: 

Example 1: Using Just Constructor
var timeStamp = (new Date()).getTime();
console.log(timeStamp)
// Output(in milliseconds) ==> 1644321718881

Example2: Using Date.now()

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: 

Example2: Using Date.now()
var timeStamp = Date.now();
console.log(timeStamp)
// Output(in milliseconds) ==> 1644323353482

Example 3: Using +new Date()

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: 

Example 3: Using + new Date()
var timeStamp = (+ new Date());
console.log(timeStamp)
// Output(in milliseconds) ==> 1644323353482

Example 4: Using new Date().valueOf()

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: 

Example 4: Using new Date().valueOf()
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!


Related Posts



Comments

Recent Posts
Tags