How To Get The Current Date And Time In Seconds Using JavaScript

get the current Date and Time in seconds using JavaScript

We will learn how to get the current Date and Time in Seconds using JavaScript using three different approaches. Each approach is rather straightforward, and you can choose whichever you want.

Get the current Date and Time in Seconds using JavaScript

Get the current Date in seconds 

The getTime() function returns the millisecond (integer value) since the date January 1, 1970, UTC. You can also use this method to get the current date and time in seconds.

Syntax:

getTime()

This method can return the epoch (unix timestamp) of a Date and Time object in milliseconds.

today = new Date()
console.log("DateTime in millisec: "+ today.getTime())
console.log("DateTime in sec:"+today.getTime()/1000)

Output: 

DateTime in millisec: 1664538697476
DateTime in sec:1664538697.476

The above example uses the getTime() method to get the date and time in milliseconds of a Date object. However, there is no method to get the date and time in seconds, so we have to use the time in milliseconds and multiply it by 1000 to convert it to ‘second’ measurement.

Get the current Time in seconds

We can use the method getHours(), getMinutes(), getSeconds() to get the current hour, minute, second respectively. Generally, a day has 24 hours, so it has 24*60*60 = 86400 seconds. If you want to calculate the current time in seconds, you can convert the hour and the minute to seconds and then add it together with the second value. You will receive a time which is not larger than 86400 by the following function:

function TimetoSeconds(date){
    return 60*60*date.getHours() + 60*date.getMinutes() + 60*date.getSeconds()
}

Sample test case:

today = new Date()
console.log(TimetoSeconds(today))

Output:

71940

If this approach is not what you are looking for, maybe you should consider converting the current Date and Time string (time format) to seconds. Take a look at the below approach:

Convert the current Date and Time from String to seconds

A date object has a method toString() which help you to convert the date to time-formatted string. Similarly, the string representing a date time format can be converted back to milliseconds and also seconds with the help of JavaScript Date objects. For example:

today = new Date();
string = today.toString();
console.log(string);
today = new Date(string);
console.log("Date time in sec:"+today.getTime()/1000)

Output: 

Fri Sep 30 2022 19:26:16 GMT+0700
Date time in sec:1664540776

The logic behind this method is straightforward because you don’t have a date object. Instead, you have a string representing it, so you cannot follow the instructions in the first and second approaches. All you have to do is to convert the string back to the date object to convert to seconds

Summary

We have learned how to get the current Date and Time in seconds using JavaScript using three different solutions. It would help if you considered each approach’s pros and cons. We hope you will be successful using our tutorial.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *