Open In App

How to get the current date and time in seconds ?

Last Updated : 20 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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)


Previous Article
Next Article

Similar Reads

How to get the current Date and Time in PHP ?
The purpose of the article is to get the current Date and Time in PHP. It is performed by using a simple in-built PHP function date(). The Date is an inbuilt function used to format the timestamp. The computer stores date and time in UNIX timestamp. This time is measured as a number of seconds since Jan 1, 1970. As this is difficult for humans to r
2 min read
How to get current date and time in firebase using ReactJS ?
This article provides a detailed walkthrough on obtaining the current date and time in a Firebase environment using ReactJS. Readers will gain insights into the implementation process, enabling date and time functionalities into their React projects powered by Firebase. Prerequisites:Node JS or NPMReact JSFirebaseSteps to Create React Application A
2 min read
How to validate if input date (end date) in input field must be after a given date (start date) using express-validator ?
In HTML forms, we often required validation of different types. Validate existing email, validate password length, validate confirm password, validate to allow only integer inputs, these are some examples of validation. In certain cases, we want the user to type a date that must come after some given date(Ex. 'end date' must be after 'start date')
5 min read
How to validate if input date (start date) in input field must be before a given date (end date) using express-validator ?
In HTML forms, we often required validation of different types. Validate existing email, validate password length, validate confirm password, validate to allow only integer inputs, these are some examples of validation. In certain cases, we want the user to type a date that must come before some given date (Ex: 'start date' must come before 'end da
5 min read
How to convert UTC date time into local date time using JavaScript ?
Given an UTC date and the task is to convert UTC date time into local date-time using JavaScript toLocaleString() function. Syntax: var theDate = new Date(Date.parse('DATE_IN_UTC')) theDate.toLocaleString() Example 1: This example converts UTC date time into local date time using JavaScript. C/C++ Code <body> <h1 style="color:green;
2 min read
How to get the first and last date of current month using JavaScript ?
Given a month and the task is to determine the first and last day of that month in proper format using JavaScript. JavaScript getDate() Method: This method returns the day of the month (from 1 to 31) for the defined date. Syntax: Date.getDate() Parameters: This method does not accept any parameters. Return value: It returns a number from 1 to 31, r
3 min read
How to get current formatted date dd/mm/yyyy in Javascript and append it to an input?
The current date in "dd/mm/yyyy" format represents the day, month, and year according to the Gregorian calendar,To obtain the current date in "dd/mm/yyyy" format using JavaScript, utilize the `Date` object methods to extract day, month, and year components, format them accordingly, and then append the formatted date to an input field. Here, we have
3 min read
JavaScript Adding seconds to Date object
Given a date object and the task is to add seconds to the date using JavaScript. We will use a few methods to add seconds to the date, that are discussed below: JavaScript getSeconds() Method: This method returns the Seconds(from 0 to 59) of the provided date and time. Syntax: Date.getSeconds() Parameters: This method does not accept any parameters
3 min read
How to get the current date in JavaScript ?
In this article, we will see how to get the current date using the built-in methods in JavaScript & will see their implementation through the examples. There are 2 methods to perform this task: Using Date().toDateString() MethodUsing their respective method for fetching day, month, and year.There are several other ways to perform this task but
3 min read
How to get current formatted date dd/mm/yyyy in JavaScript ?
In this article, we will see how to get the format current date in dd/mm/yyyy format by using JavaScript. We're going to discuss a few methods. The first few methods to know These are the methods that can be used in solving this problem: Table of Content JavaScript getDate() MethodJavaScript getFullYear() MethodJavaScript getMonth() MethodJavaScrip
3 min read