. Advertisement .
..3..
. Advertisement .
..4..
This article today will walk you through how to convert JavaScript timestamps to date, which is quite a considerable differential characteristic compared to other languages.
That said, JavaScript doesn’t have a counterpart for the function strftime as others do. This language, on the other hand, offers a variety of methods to display date and time in a comprehensible format for humans.
The implementation we are about to mention in this instruction is one of the most accessible approaches to get you neatly to where you head toward. Let’s dig in and check out the way to nail such a process!
Javascript Timestamps To Date: Step-by-step Guidance
You can convert a Unix timestamp to dateBy performing the three methods as follow:
Method #1: Employing The Newly Produced Milliseconds Value To Generate The New Date() Constructor Function With A Date Object.
The time in milliseconds when an object is formed is returned by new Date()
as it is constructed from the Date()
class.
The epoch timestamp can be sent to the constructor of the Date class if we require an object from that class at a specified time.
Have a look at the illustration below to grasp more.
Running the code:
var timestamp = 1607110465663
var date = new Date(timestamp);
console.log(date.getTime())
console.log(date)
Output:
1607110465663
2020-12-04T19:34:25.663Z
Method #2: Utilize Your Picked Format To Display The Date
The Date class in JavaScript offers a variety of ways to express dates in the desired manner, including:
- getSeconds(): return the accurate seconds ranging from 0 to 59 at that point.
- getMinutes(): return the accurate minutes ranging from 0 to 59 at that point.
- getHours(): return the accurate hour in the period of 24 hours for that point.
- getDate(): return the day ranging from 1 to 31 at that point.
- getFullYear(): return the year under the format of 4 digits.
- getMonth(): return the month number ranging from 0 to 11 at that point.
Check out the example below for further understanding.
Running the code:
var timestamp = 1607110465663
var date = new Date(timestamp);
console.log("Date: "+date.getDate()+
"/"+(date.getMonth()+1)+
"/"+date.getFullYear()+
" "+date.getHours()+
":"+date.getMinutes()+
":"+date.getSeconds());
Output:
Date: 4/12/2020 19:34:25
Step 3: Multiplying The Unix Timestamp By 1000
The Unix timestamp may be converted to a JavaScript timestamp by multiplying it by 1000 since the JavaScript Date timestamp is in milliseconds while the Unix timestamp is in seconds. The JavaScript timestamp is 1607110465000 if the Unix timestamp is 1607110465.
The conversion of the Unix timestamp to the JavaScript Date timestamp is shown in the example below.
Running the code:
var unixTimestamp = 62678980
var date = new Date(unixTimestamp*1000);
console.log("Unix Timestamp:",unixTimestamp)
console.log("Date Timestamp:",date.getTime())
console.log(date)
console.log("Date: "+date.getDate()+
"/"+(date.getMonth()+1)+
"/"+date.getFullYear()+
" "+date.getHours()+
":"+date.getMinutes()+
":"+date.getSeconds());
Output:
Unix Timestamp: 62678980
Date Timestamp: 62678980000
Mon Dec 27 1971 12:49:40 GMT+0200 (Eastern European Standard Time)
Date: 27/12/1971 12:49:40
The Bottom Line
This tutorial has introduced to you all the gist of how to convert timestamp to date in JavaScript. Hopefully, you can rack up the most out of it. See then!
Leave a comment