. Advertisement .
..3..
. Advertisement .
..4..
JavaScript is all about interactive elements using different programs that are not even grabbing users’ attention, but also develop their interest. Convert a UNIX timestamp to time using JavaScript is also a type of program that makes the time in a user-readable format.
The conversion is required from UNIX timestamp to time as the API request-response is always in UNIX format. And, it is required to convert it to time, which is a user-readable format that can be displayed on the screen.
Let’s take a look at how to convert UNIX timestamp to time
How to Convert a UNIX timestamp to time using JavaScript?
As we have discussed why we need to convert a UNIX timestamp to time. There are a few methods to get the desired time displayed
- Create a ‘
date
’ object - The
getHours()
,getMinutes()
, andgetSeconds()
method - The ‘
YYYY-MM-DD hh:mm:ss
’ format, optional
Using date Object with getHours(), getMinutes(), and getSeconds() method
In the date object method, we use the timestamp and convert it into hours, minutes, and seconds to make it user-friendly time. The date.getHours()
, date.getMinutes()
, and date.getSeconds()
is used to return the hours, minutes, and seconds for the specified date. Let’s take a look at the code with string
const getString = (number) => number < 10 ? '0' + number : number;
// Converts unix timestamp in seconds to HH:mm:ss string
const getTime = (timestamp) => {
const date = new Date(1000 * timestamp);
const hours = getString(date.getHours());
const minutes = getString(date.getMinutes());
const seconds = getString(date.getSeconds());
return `${hours}:${minutes}:${seconds}`;
};
// Usage example:
const timestamp = 1610796674; // unix timestamp in seconds
console.log(getTime(timestamp));
Output
6:31:14
The ‘YYYY-MM-DD hh:mm:ss’ format
In this format, we can have the time converted into the year, month, and days along with hours, minutes, and seconds. This ‘YYYY-MM-DD hh:mm:ss
’ format shows the complete time to users in a readable format. When working with date, the time always shows in UNIX timestamp, which looks something like this
1610796674
Let’s check out how to convert it into a user-readable format
function convertUnixTime(unix) {
let a = new Date(unix * 1000),
year = a.getFullYear(),
months = ['January','February','March','April','May','June','July','August','September','October','November','December'],
month = months[a.getMonth()],
date = a.getDate(),
hour = a.getHours(),
min = a.getMinutes() < 10 ? '0' + a.getMinutes() : a.getMinutes(),
sec = a.getSeconds() < 10 ? '0' + a.getSeconds() : a.getSeconds();
return `${month} ${date}, ${year}, ${hour}:${min}:${sec}`;
}
// Pass in a UNIX timestamp (milliseconds since January 1, 1970)
console.log(convertUnixTime(1610796674));
Output
January 16, 2021, 6:31:14
You can also customize the code to get the time in different formats if you want.
Conclusion
Here, we have highlighted the importance of timestamp conversion and how to convert a UNIX timestamp to time using JavaScript. The user-readable format is required if you want it to be displayed on the web page. Methods have been discussed here, and you can always have the option to customize the time the way you want to be shown on the page for users.
With this, I wish you happy coding!
Leave a comment