This is what I did for the Instagram API. converted timestamp with date method by multiplying by 1000. and then added all entity individually like [year, months, etc]
created the custom month list name and mapped it with getMonth[]
method which returns the index of the month.
convertStampDate[unixtimestamp]{
// Months array
var months_arr = ['January','February','March','April','May','June','July','August','September','October','November','December'];
// Convert timestamp to milliseconds
var date = new Date[unixtimestamp*1000];
// Year
var year = date.getFullYear[];
// Month
var month = months_arr[date.getMonth[]];
// Day
var day = date.getDate[];
// Hours
var hours = date.getHours[];
// Minutes
var minutes = "0" + date.getMinutes[];
// Seconds
var seconds = "0" + date.getSeconds[];
// Display date time in MM-dd-yyyy h:m:s format
var fulldate = month+' '+day+'-'+year+' '+hours + ':' + minutes.substr[-2] + ':' + seconds.substr[-2];
// final date
var convdataTime = month+' '+day;
return convdataTime;
}
Call with stamp argument convertStampDate['1382086394000']
and that's it.
Created: December-05, 2020 | Updated: December-10, 2020 This tutorial will explain how we can convert the Unix timestamp to date in JavaScript. Unix timestamp is the time elapsed since the 1, Jan 1970 00:00:00 UTC, represented in seconds. The JavaScript When we create a new object from the Output: The Output: Since the JavaScript The following example demonstrates how we can convert the Unix timestamp to JavaScript Date
object contains the representation for the time elapsed since the 1, Jan 1970 00:00:00 UTC in milliseconds.Convert Unix Timestamp to
Date
in JavaScriptDate[]
class using new Date[]
, it returns the time in milliseconds when it is created. If we need to get an object from the Date
class at a specific point of time, we can pass the epoch timestamp to that class’s constructor.var timestamp = 1607110465663
var date = new Date[timestamp];
console.log[date.getTime[]]
console.log[date]
1607110465663
2020-12-04T19:34:25.663Z
Date
class provides many methods to represent the Date
in the preferred format like:getDate[]
returns the day of the calendar month 1 to 31
at that time.getMonth[]
returns the month number 0 to 11
at that time.getFullYear[]
returns the year in 4-digits
format.getHours[]
returns the exact hour in 24-hour
format for that time.getMinutes[]
returns the exact minutes 0 to 59
at that time.getSeconds[]
returns the exact seconds 0 to 59
at that time.var timestamp = 1607110465663
var date = new Date[timestamp];
console.log["Date: "+date.getDate[]+
"/"+[date.getMonth[]+1]+
"/"+date.getFullYear[]+
" "+date.getHours[]+
":"+date.getMinutes[]+
":"+date.getSeconds[]];
Date: 4/12/2020 19:34:25
Date
timestamp is in the unit of millisecond while the Unix timestamp is in the unit of second, we can multiply 1000 to convert
the Unix timestamp to JavaScript timestamp. If the Unix timestamp is 1607110465
, then the JavaScript timestamp is 1607110465000
.Date
timestamp.
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
Related Article - JavaScript Date
How do you convert a Unix timestamp value into a human-readable date using vanilla JavaScript?
You can convert the Unix timestamp to a date string by following these three steps:
- Convert the unix timestamp into milliseconds by multiplying it by
1000
- Use the newly created milliseconds value to create a date object with the
new Date[]
constructor method - Use the
.toLocaleString[]
function to convert the date object into human-friendly date strings
In this article, we'll walk you through each of those steps.
Let's get started!
Table of Contents
- Convert the Unix Timestamp to Milliseconds
- Create a Date Object Using new Date[]
- Create Human-Friendly Date Strings With .toLocaleString[]
Convert the Unix Timestamp to Milliseconds
Since the new Date[]
function needs to be supplied with a milliseconds value, we need to first convert our given Unix timestamp to milliseconds. We can do this by simply multiplying the Unix timestamp by 1000
.
Unix time is the number of seconds that have elapsed since the Unix epoch, which is the time 00:00:00 UTC
on 1 January 1970
. It's most commonly used to create a running total of seconds when
interacting with computers.
Therefore, a Unix timestamp is simply the number of seconds between a specific date and the original Unix Epoch date.
Measuring time using Unix timestamps is particularly useful because it is the same for everyone around the globe at all times since they don't observe timezones. This can be very useful for dealing with dated information on both the server and client-side of applications.
So, let's write some code to convert a Unix timestamp to milliseconds:
JavaScript
Copy
const unixTimestamp = 1575909015
const milliseconds = unixTimestamp * 1000 // 1575909015000
Feel free to substitute your own Unix timestamp in the code above.
In the next section, we'll put to use that milliseconds value we just created.
Create a Date Object Using new Date[]
Now that we have a milliseconds value, we can create a new Date[]
object.
The Date
object instance we create will represent a single moment in time and will hold data on the year, month,
day, hour, minute, and second for that moment in time.
Let's add on to the code we already wrote in the last section. To create the Date
object, make your code look like this:
JavaScript
Copy
const unixTimestamp = 1575909015
const milliseconds = 1575909015 * 1000 // 1575909015000
const dateObject = new Date[milliseconds]
We use the new Date[]
constructor and pass to it the milliseconds
variable we created in the last section.
As a result, we're left with a newly created dateObject
variable that represents the Date
object instance. We'll use this in the next
section.
Create Human-Friendly Date Strings With .toLocaleString[]
Now that we have a Date
object to work with, we can start creating some human-friendly date strings.
Using the .toLocaleString[]
function is one really easy way to do this. The function can be called on a data object and will return a string with a language sensitive representation of the date portion of the given date object.
Here's what a simple code example looks like [adding on to the code we have written in the past sections]:
JavaScript
Copy
const unixTimestamp = 1575909015
const milliseconds = 1575909015 * 1000 // 1575909015000
const dateObject = new Date[milliseconds]
const humanDateFormat = dateObject.toLocaleString[] //2019-12-9 10:30:15
As you can see, we created a human-friendly date string by calling the .toLocaleString[]
on the dateObject
we created in the last section.
Here are some examples of how you can use the .toLocaleString[]
to return strings of specific components of the date by passing different arguments to the .toLocaleString[]
function:
JavaScript
Copy
const unixTimestamp = 1575909015
const milliseconds = 1575909015 * 1000 // 1575909015000
const dateObject = new Date[milliseconds]
const humanDateFormat = dateObject.toLocaleString[] //2019-12-9 10:30:15
dateObject.toLocaleString["en-US", {weekday: "long"}] // Monday
dateObject.toLocaleString["en-US", {month: "long"}] // December
dateObject.toLocaleString["en-US", {day: "numeric"}] // 9
dateObject.toLocaleString["en-US", {year: "numeric"}] // 2019
dateObject.toLocaleString["en-US", {hour: "numeric"}] // 10 AM
dateObject.toLocaleString["en-US", {minute: "numeric"}] // 30
dateObject.toLocaleString["en-US", {second: "numeric"}] // 15
dateObject.toLocaleString["en-US", {timeZoneName: "short"}] // 12/9/2019, 10:30:15 AM CST
The
.toLocaleString
takes a locales string parameter that alters results based on language and geography. In the example above, we used the "en-US"
locale tag. You can learn more about other values you can use instead here.
We also passed an object with some options in it as well. If you want to learn more, there's some good information about those here.
That was the last step!
Conclusion
In this article, we showed you the three steps to achieve your goal: convert the Unix timestamp to milliseconds, create a Date
object using the new Date[]
constructor, and use the .toLocaleString[]
function to create human-friendly date strings.
You now know how to convert a Unix timestamp into a human-readable date using vanilla JavaScript!
Thanks for reading and happy coding!