Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How can I get time and date since epoch in JavaScript?
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 time units from 1 January 1970 since the UNIX epoch started.
The Unix epoch is the starting point (January 1, 1970, 00:00:00 UTC) used by most computer systems to measure time. JavaScript provides several methods to calculate time elapsed since this date.
Using the Date() Constructor
In this approach, we will learn to get the current date from the UNIX epoch that started on 1 January 1970. In JavaScript, the Date() class contains methods and properties for date and time manipulation. You can create an object of the Date() class and invoke all methods of the Date() class on the object.
Syntax
let date = new Date(); // get current date let totalMilliSeconds = date / 1; // get total milliseconds since epoch
Example
In the example below, we have created an 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.
<html>
<head>
</head>
<body>
<h2>Get date and time since Unix epoch 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 epoch 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 epoch
output2.innerHTML += date / 1;
</script>
</body>
</html>
Get current Date using the new Date() object. Wed Dec 11 2024 15:30:45 GMT+0000 (UTC) Get total number of milliseconds from epoch using new Date() method. 1733932245000
Using the Date.valueOf() Method
In this method, we will use the valueOf() method of the Date class. When you invoke the valueOf() method on a Date object, it returns the total number of milliseconds since the epoch. We can convert milliseconds to hours, days, etc.
Syntax
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 an object of the Date class and found the total number of milliseconds since the epoch using the valueOf() method. Furthermore, we have calculated the total number of hours since the epoch.
<html>
<head>
</head>
<body>
<h2>Get date and time since Unix epoch started in JavaScript.</h2>
<h4>Get total number of milliseconds from epoch using <i>Date.valueOf()</i> method.</h4>
<p id="output1"></p>
<h4>Get total number of hours from epoch 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>
Get total number of milliseconds from epoch using Date.valueOf() method. 1733932245000 Get total number of hours from epoch using Date.valueOf() method. 481648
Using Date.now() Method
The simplest way to get the current timestamp is using Date.now(), which directly returns milliseconds since epoch without creating a Date object.
Example
<html>
<head>
</head>
<body>
<h2>Get timestamp using Date.now()</h2>
<h4>Milliseconds since epoch: <span id="output1"></span></h4>
<h4>Days since epoch: <span id="output2"></span></h4>
<script>
let milliseconds = Date.now();
document.getElementById("output1").innerHTML = milliseconds;
// Convert to days
let days = Math.floor(milliseconds / (1000 * 60 * 60 * 24));
document.getElementById("output2").innerHTML = days;
</script>
</body>
</html>
Milliseconds since epoch: 1733932245000 Days since epoch: 20061
Using the Moment.js Library
Moment.js is a popular JavaScript library for date manipulation. The moment() method provides additional functionality for working with dates and times.
Syntax
let milliseconds = moment().valueOf(); // dividing milliseconds by 1000 to get seconds let seconds = milliseconds / 1000; // dividing seconds by 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 use the moment() method. We have created a new date object using moment(), which returns milliseconds since epoch.
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.3/moment-with-locales.min.js"></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 epoch using <i>moment()</i> method.</h4>
<p id="output2"></p>
<script>
let output1 = document.getElementById("output1");
let output2 = document.getElementById("output2");
let milliseconds = moment().valueOf();
// get the seconds
let seconds = Math.floor(milliseconds / 1000);
output1.innerHTML += seconds;
// get the total days since epoch
output2.innerHTML += Math.floor(seconds / (60 * 60 * 24));
</script>
</body>
</html>
Comparison of Methods
| Method | Returns | Performance | Dependency |
|---|---|---|---|
Date.now() |
Milliseconds | Fastest | None |
new Date().valueOf() |
Milliseconds | Good | None |
moment().valueOf() |
Milliseconds | Slower | Moment.js library |
Conclusion
JavaScript offers multiple ways to get time since epoch. For simple timestamp needs, use Date.now() as it's the fastest. Use Moment.js when you need advanced date manipulation features, though native JavaScript Date methods are sufficient for basic epoch calculations.
