How to Convert Unix Timestamp to Universal Time in Javascript
In this tutorial, you will learn how to convert Unix timestamp to universal time in javascript. Unix timestamp is the number of seconds that’s have been passed since January 1st, 1970 at UTC. UTC stands for Universal Time Coordinated. Before 1972, it was known as Greenwich Mean Time.
Unix timestamp does not change no matter where you are located in the world. This makes it easy for the computer systems to convert universal time into your local time. In javascript, we are going to use the Date
constructor to convert the Unix timestamp to universal time.
The following example is pretty straightforward. We have timestamp 10999285
and we will convert it into universal time. Please have a look over the code example and steps given below.
HTML & CSS
- We have 3 elements in the HTML file (
div
,button
, andh1
). Thediv
element is just a wrapper for the rest of the elements. - We have done some basic styling using CSS and added the link to our
style.css
stylesheet inside thehead
element. - We have also included our javascript file
script.js
with ascript
tag at the bottom.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <link rel="stylesheet" href="style.css"> <title>Document</title> </head> <body> <div> <button>Get</button> <h1>Result</h1> </div> <script src="script.js"></script> </body> </html>
body { text-align: center; } div { display: inline-block; } button{ display: inline-block; padding: 10px 20px; }
Javascript
- We have selected 2 elements (
button
andh1
) using thedocument.querySelector()
method and stored them inbtn
andresult
variables. - We have created one global variable
timestamp
and assigned10999285
as a value. - We have attached the
click
event listener to thebutton
element. - Inside the
click
event handler, we are making use of theDate
constructor. We are converting the timestamp in seconds to milliseconds by multiplying it by 1000 and supplying it to theDate
constructor as a parameter. As a result, we are getting the date object which we are storing indateObj
variable. - We are using
getUTCHours()
,getUTCMinutes()
,getUTCSeconds()
functions to get hours, minutes, and seconds of universal time. - We are using
padStart(2, 0)
to make sure always 2 digits should be displayed on the screen. If we have only one digit, then we will prepend 0 to it. - We are simply creating a template string and displaying it on the screen inside the
h1
element.
let btnGet = document.querySelector('button'); let result = document.querySelector('h1'); //07:21:25 let timestamp = 10999285; btnGet.addEventListener('click', () => { let dateObj = new Date(timestamp * 1000); let hours = dateObj.getUTCHours().toString().padStart(2,0); let minutes = dateObj.getUTCMinutes().toString().padStart(2,0); let seconds = dateObj.getUTCSeconds().toString().padStart(2,0); result.innerText = `${hours}:${minutes}:${seconds}`; });