Thursday, January 22, 2026
HomeLanguagesJavascriptHow to get the current date and time in seconds ?

How to get the current date and time in seconds ?

In this article, we will learn how to get the current date & time in seconds using JavaScript build-in methods. We will perform it in 2 ways:

  • Using Date.now() method
  • Using new Date.getTime() method

Method 1: Using Date.now() Method

The Date.now() method returns the number of milliseconds elapsed since January 1, 1970, 00:00:00 UTC. This is known as the epoch time. It may be used for timestamping as an order of events could easily be checked by comparing the timestamps. The returned milliseconds can be converted to seconds by dividing the value by 1000 and then using the Math.round() function to round the value. This is done to prevent inconsistencies due to the float values.

Syntax:

Date.now();

Parameters: This method does not accept any parameter.

Return Values: It returns the number of milliseconds elapsed since January 1, 1970, 00:00:00 UTC.

Example: This example describes getting the current date & time using Date.now() method.

Javascript




let dateInMillisecs = Date.now();
 
// Rounding the value to prevent inconsistencies
// due to floating points
let dateInSecs = Math.round(dateInMillisecs / 1000);
let dateInWords = new Date(dateInMillisecs);
 
console.log(dateInMillisecs);
console.log(dateInSecs);
console.log(dateInWords);


Output

1686709618881
1686709619
Date Wed Jun 14 2023 07:56:58 GMT+0530 (India Standard Time)

Method 2: Using new Date.getTime() Method

The Date.getTime() method returns the number of milliseconds elapsed since January 1, 1970, 00:00:00 UTC. It always uses the UTC for time representation. It has to be initialized with the new keyword, unlike the Date.now() method. The returned milliseconds can be converted to seconds by dividing the value by 1000 and then using the Math.round() function to round the value. This is done to prevent inconsistencies due to the float values.

Syntax:

new Date().getTime();

Parameters: This method does not accept any parameter. 

Return type: A numeric value equal to no of milliseconds since Unix Epoch.

Example: This example describes getting the current date & time using Date.getTime() method.

Javascript




let dateInMillisecs = new Date().getTime();
 
// Rounding the value to prevent inconsistencies
// due to floating points
let dateInSecs = Math.round(dateInMillisecs / 1000);
let dateInWords = new Date(dateInMillisecs);
 
console.log(dateInMillisecs);
console.log(dateInSecs);
console.log(dateInWords);


Output

1686709710791
1686709711
Date Wed Jun 14 2023 07:58:30 GMT+0530 (India Standard Time)
RELATED ARTICLES

Most Popular

Dominic
32475 POSTS0 COMMENTS
Milvus
119 POSTS0 COMMENTS
Nango Kala
6847 POSTS0 COMMENTS
Nicole Veronica
11977 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12064 POSTS0 COMMENTS
Shaida Kate Naidoo
6986 POSTS0 COMMENTS
Ted Musemwa
7220 POSTS0 COMMENTS
Thapelo Manthata
6933 POSTS0 COMMENTS
Umr Jansen
6912 POSTS0 COMMENTS