How can I get days since epoch in JavaScript?


In this tutorial, we are going to learn about how to calculate days from epoch using JavaScript. Here, we are going to use the Math.abs() and the Math.floor() methods to calculate days. First, we are going to learn about what an epoch is.

What is Epoch?

It is also referred to as epoch time or Unix time or Posix time. In a determining context, an epoch is the data and time relative to which a computer's clock and timestamps are determined.

An epoch time is the number of seconds that have elapsed since Jan 1st, 1970 at midnight UTC time and minus leap seconds. Practically speaking, the epoch is the time in seconds that have elapsed since computers started counting. This epoch traditionally corresponds to 0 hours, 0 minutes and 0 seconds (00 : 00 : 00) Coordinated Universal Time (UTC) on a specific date, which varies from system to system.

Why is epoch required even if we have a date?

Converting a date and time into an epoch value makes it easier to find the difference, add and subtract from a time value. Epoch is useful in computer programming because we can use it to compare dates mathematically, which helps us to write programming logic with it by converting a human-readable data format into its equivalent epoch value.

Below table to determine the time period for epoch second equivalent.

Time Epoch second equivalent
1 minute 60
30 minutes 1800
1 hour 3600
1 day 86,400
1 week 604,800
2 weeks 12,09,600
3 weeks 18,14,400
1 month 24,19,200
6 months 14,515,200
1 year 29,030,400
1 decade 290,304,000

For example, you could convert the time to an epoch and subtract it from another epoch value to quickly determine the difference. If the difference was 95,400 and you used the above chart or a math formula to find the difference is 1 day and 2 and half hours (86400 + 3600+ 3600 + 1800 = 95,400).

Steps to get days since the epoch

STEP 1 - Define the current date.

STEP 2 - Define the epoch date.

STEP 3 - Find the absolute difference between the current and epoch dates divided by 1000.

STEP 4 - Find the total number of days between two dates. To find it we divide the result in step 3 by 86400.

STEP 5 - Display the number of days since the epoch.

Example

In the example below, we get the number of days since the epoch in the JavaScript

<!DOCTYPE html>
<html>
<body>
   <h1>Calculating days since epoch</h1>
   <p>Current time is -
      <span id="currentDate"></span>
   </p>
   <p>Epoch time is -
      <span id="epochDate"></span>
   </p>
   <p>Number days -
      <span id="difference"></span>
   </p>
   <script>
      var currentDate, epochDate;
      currentDate = new Date();
      document.getElementById('currentDate').innerHTML=currentDate;
      var epochDate = new Date(new Date().getTime() / 1000);
      document.getElementById('epochDate').innerHTML=epochDate;
      var res = Math.abs(currentDate - epochDate) / 1000;
      
      // get total days between two dates
      var difference = Math.floor(res / 86400);
      document.getElementById('difference').innerHTML=difference;
   </script>
</body>
</html>

Here, the getTime() method, returns the number of milliseconds. If we divide by 1000, then it returns in a number of seconds since epoch. Here, we divided the difference by 86400 because we want to calculate it in the form of days.

Math.abs() method returns absolute value of numbers. That is, it returns value if value is positive or zero, and the negation of value if value is negative and NaN if the value is not a number. Hope this tutorial gives us knowledge on epoch and calculating days since epoch.

Updated on: 08-Dec-2022

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements