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 to convert epoch Date to meaningful JavaScript date?
In this tutorial, we will learn to convert the epoch Date to a meaningful JavaScript date. The epoch date is a representation of the date in milliseconds, and it is a total number of milliseconds since 1st January 1970 since the UNIX epochs started.
Users can convert the total number of milliseconds into the meaningful date string using various approaches explained below.
Using the new Date() Object
In this approach, we will create the object of the Date class. The constructor of the Date() class takes the total number of milliseconds since the epoch started and converts it into the meaningful JavaScript date.
Syntax
Users can follow the syntax below to create the new object of the Date class by passing the total number of milliseconds as a parameter.
let milliseconds = 2302345654324; // epoch date let date = new Date(milliseconds);
Example
In the example below, we have created the variable to store the total number of milliseconds and passed it as a parameter of the Date() constructor. The date object returns the date string; which users can see in the output.
<html>
<head>
</head>
<body>
<h2>Converting epoch date to meaningful JavaScript date.</h2>
<h4>Convert milliseconds since epoch to JavaScript Date using the <i>new Date()</i> object.</h4>
<p id="output1"></p>
<script>
let output1 = document.getElementById("output1");
let milliseconds = 2302345654324;
let date = new Date(milliseconds);
output1.innerHTML += "date for total " + milliseconds + " milliseconds is : " + date + " <br/> ";
date = new Date(902976543332);
output1.innerHTML += "date for total 902976543332" + " milliseconds is : " + date + " <br/> ";
</script>
</body>
</html>
date for total 2302345654324 milliseconds is : Sun Dec 17 2042 07:14:14 GMT+0530 (India Standard Time) date for total 902976543332 milliseconds is : Sat Aug 19 1998 19:55:43 GMT+0530 (India Standard Time)
Using Various Methods of the Date Class
In this approach, we will create the object of the date class and pass the total milliseconds since epoch as a parameter. After that, we will get the year, month, date, etc. separately from the date object and format the string according to our need.
Users can use the getFullYear() method to get the year. The getMonth() returns the number of months between 0 to 11. So, we need to add 1 to returned value to get the proper value of the month.
With the year and month, users can get all the necessary things to create the data using different methods, and for all methods, users can follow the syntax below.
Syntax
let milliseconds = 1348755654324; let myDate = new Date(milliseconds); // using various methods of Date class to get year, date, month, hours, minutes, and seconds. let dateStr = myDate.getFullYear() + "/" + (myDate.getMonth() + 1) + "/" + myDate.getDate() + " " + myDate.getHours() + ":" + myDate.getMinutes() + ":" + myDate.getSeconds()
Example
In the example below, we have used all methods given in the syntax to get every separate part of the date and formatted the date string. We have created the object of the date class and invoked all the above methods by taking the date object as a reference.
<html>
<head>
</head>
<body>
<h2>Converting epoch date to meaningful JavaScript date.</h2>
<h4>Convert milliseconds since epoch to JavaScript Date using various methods of Date class.</h4>
<p id="output1"></p>
<script>
let output1 = document.getElementById("output1");
let milliseconds = 1348755654324;
let myDate = new Date(milliseconds);
let dateStr = myDate.getFullYear() + "/" + (myDate.getMonth() + 1) + "/" + myDate.getDate() + " " + myDate.getHours() + ":" + myDate.getMinutes() + ":" + myDate.getSeconds();
output1.innerHTML += "date string for " + milliseconds + " milliseconds is : " + dateStr + " <br/> ";
</script>
</body>
</html>
date string for 1348755654324 milliseconds is : 2012/9/27 18:57:34
Common Date Methods
Here are the most commonly used Date methods for extracting specific parts:
| Method | Returns | Range |
|---|---|---|
getFullYear() |
Full year | 4-digit year |
getMonth() |
Month | 0-11 (add 1 for actual month) |
getDate() |
Day of month | 1-31 |
getHours() |
Hour | 0-23 |
getMinutes() |
Minutes | 0-59 |
getSeconds() |
Seconds | 0-59 |
Conclusion
Converting epoch timestamps to readable dates is straightforward using JavaScript's Date constructor. Use new Date(milliseconds) for quick conversion or individual Date methods for custom formatting.
