Timestamp to Date in JavaScript
Posted by Superadmin on May 02 2023 13:58:04

Timestamp to Date in JavaScript

Timestamp to Date in JavaScript

Introduction to Timestamp to Date in JavaScript

Timestamp to date in JavaScript is one of the most commonly used scenarios by the programmers. In JavaScript, programmers can retrieve the individual elements of the full-fledged date using the various methods provided by the Date() object. Once all the elements are extracted, the user can print or use them in the code according to its own preferences. One thing to note while working on the JavaScript date object is that it takes the timestamp in milliseconds to convert the Unix timestamp into a JavaScript timestamp. Therefore, different programmers use different approaches in order to extract the date and time in their specified format and the timezones.

Syntax

Below given is the basic syntax of the conversion of the timestamp to extract the date and time in JavaScript using the various methods provided by the Date() object:

<script>
// converting timestamp into milliseconds
var t_stamp = xyz;
var t_stamp_ms = t_stamp *1000;
//creating the date object
var obj = date(t_stamp_ms);
var hours = getHours(obj);
var min = getMinutes(obj);
…
…
…
…
//printing values on the console
</script>

Where xyz is a numerical UNIX timestamp value.

How to Convert Timestamp to Date in JavaScript?

In JavaScript, unix timestamp can be converted to the date and time using the Date() object. In order to retrieve the Date and time values from the timestamp provided by the programmer, it is important for the programmer to understand the various methods which are provided by the Date() object in JavaScript to extract the individual elements of the whole timestamp.

Below given are some of the steps which need to be performed in order to convert the timestamp to date and time in JavaScript:

Example of Timestamp to Date in JavaScript

Given below is the example of Timestamp to Date in JavaScript:

Code:

<!DOCTYPE html>
<html>
<body>
<h3>JavaScript Date Function </h3>
<p id="demo"></p>
<p id ="demo1"> </p>
<p id = "demo2"> </p>
<script>
// unix timestamp
var timestamp = 890099870;
// convert unix timestamp to milliseconds
var timestamp_ms = timestamp * 1000;
// initializing the Date object
var d_obj = new Date(timestamp_ms);
// extracting date from the date object as 2 digit
var date = ("0" + d_obj.getDate()).slice(-2);
// extracting hours from the date object as 2 digit
var hrs = ("0" + d_obj.getHours()).slice(-2);
// extracting minutes from the date object as 2 digit
var mins = ("0" + d_obj.getMinutes()).slice(-2);
// extracting the seconds from the date object as 2 digit
var sec = ("0" + d_obj.getSeconds()).slice(-2);
// extracting year from the date object as 4 digit
var yr = d_obj.getFullYear();
// extracting month from the date object as 2 digit
var mth = ("0" + (d_obj.getMonth() + 1)).slice(-2);
//printing the values of the date and time in different formats on the console
document.getElementById("demo").innerHTML = "Extended date is: " + yr + "/" + mth + "/" + date;
document.getElementById("demo1").innerHTML = "Date in YYYY/MM/DD hh:mm:ss Format: " + yr + "/" + mth + "/" + date + " " + hrs + ":" + mins + ":" + sec;
document.getElementById("demo2").innerHTML = "Time in hours and minutes: " + hrs + ":" + mins;
</script>
</body>
</html>

Output:

Timestamp to date in JavaScript 1

Explanation:

Conclusion

The above description clearly explains how the Date() function works in JavaScript and how converting a timestamp to date in JavaScript works. The programmers widely use the date in the code to perform the various date and time-related tasks. Though JavaScript uses the browser’s timezone in order to display the time and date, we can change to different time zones using the functions provided in the JavaScript library.