How can I get time and date since epoch in JavaScript?

Front End TechnologyJavascriptWeb Development

In this tutorial, we will learn to get the date and time since epoch in JavaScript. Sometimes, programmers need to find the total number of milliseconds, seconds, days, or other things from 1 January 1970 since the UNIX epochs started.

So, users can use the Date() class of JavaScript or the Moment.js library of JavaScript to get the date and time since the epoch started.

Using the Object of Date() Class

In this approach, we will learn to get the current date from the UNIX epoch that has been started on 1 January 1970. In JavaScript, the Date() class contains the method and properties of date and time. Users can create the object of the Date() class and invoke all methods of the Date() class on the object.

Syntax

Users can follow the syntax below to get the date since the epoch.

let date = new Date( ); // get date
let totalMilliSeconds = date / 1; // get total milliseconds since epoch

Example

In the example below, we have created the object of the Date() class to get the date since the epoch. We are also finding the total number of milliseconds since the epoch by dividing the date by 1. Users can also get seconds, minutes, and hours since the epoch.

<html> <head> </head> <body> <h2> Get date and time since Unix epochs started in JavaScript. </h2> <h4> Get current Date using the <i> new Date() </i> object. </h4> <p id = "output1"> </p> <h4> Get total number of milliseconds from epochs using <i> new Date() </i> method.</h4> <p id = "output2"> </p> <script> let output1 = document.getElementById("output1"); let output2 = document.getElementById("output2"); let date = new Date(); output1.innerHTML += date; // divide date by 1 to get total milliseconds since epochs output2.innerHTML += date / 1; </script> </body> </html>

Using the Date.valueof() method

In this method, we will use the valueOf() method of the date class. When the user invokes the valueOf() method by taking the object of the date class as a reference, It returns the total number of milliseconds since the epoch. We can get total hours, days, etc., from milliseconds.

Syntax

Users can follow the syntax below to use the valueOf() method and get the total hours since the epoch started.

let milliseconds = new Date().valueOf();
// divide milliseconds by 1000 * 60 * 60 (seconds * minutes * hours) to get total hours
let hours = Math.floor( milliseconds / (1000 * 60 * 60) );

Example

In the example below, we have created the object of the date class and found the total number of milliseconds since the epoch started using the valueOf() method. Furthermore, we have found the total number of hours since the epoch using the total milliseconds.

<html> <head> </head> <body> <h2> Get date and time since Unix epoch started in JavaScript. </h2> <h4> Get total number of milliseconds from epochs using <i> Date.valueOf() </i> method. </h4> <p id = "output1"> </p> <h4> Get total number of hours from epochs using <i> Date.valueOf() </i> method. </h4> <p id = "output2"> </p> <script> let output1 = document.getElementById("output1"); let output2 = document.getElementById("output2"); let milliseconds = new Date().valueOf(); output1.innerHTML += milliseconds; output2.innerHTML += Math.floor(milliseconds / (1000 * 60 * 60)); </script> </body> </html>

Using the Moment.js moment() Method

In this method, we will use the Moment.js library, a special library to manipulate the date. The Moment.js contain the moment() method, which returns the total number of milliseconds.

Users can follow the syntax below to get the total number of milliseconds since the epoch using the Moment() method and convert it into seconds and days.

Syntax

let milliseconds = moment();
// dividing milliseconds by 1000 to get seconds
let seconds = milliseconds / 1000;
// dividing seconds with 60 * 60 *24 (seconds * minutes * hours) to get days
let days = Math.floor( seconds / (60 * 60 * 24) );

Example

In the example below, we have added the Moment.js CDN to the <head> tag to use the moment() method. We have created the new date object using the moment() method, which returns a total number of milliseconds. Also, we have converted milliseconds to seconds and days.

<html> <head> <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.3/moment-with-locales.min.js" integrity="sha512-vFABRuf5oGUaztndx4KoAEUVQnOvAIFs59y4tO0DILGWhQiFnFHiR+ZJfxLDyJlXgeut9Z07Svuvm+1Jv89w5g==" crossorigin="anonymous" referrerpolicy="no-referrer"> </script> </head> <body> <h2>Get the date and time since the Unix epoch started in JavaScript.</h2> <h4>Get total number of seconds from epoch using <i>moment()</i> method.</h4> <p id = "output1"></p> <h4> Get total number of days from epochs using <i>moment()</i> method.</h4> <p id = "output2"></p> <script> let output1 = document.getElementById("output1"); let output2 = document.getElementById("output2"); let milliseconds = moment(); // get the seconds let seconds = milliseconds / 1000; output1.innerHTML += seconds; // get the total days since epoch output2.innerHTML += Math.floor(seconds / (60 * 60 * 24)); </script> </body> </html>

Users have learned to get the time and date since epoch in JavaScript. Users can use the vanilla JavaScript's Date() class or Moment.js library. It is better to use Moment.js, as it contains more methods to manipulate the dates and times compared to the Date() class.

raja
Updated on 17-Aug-2022 12:36:50

Advertisements