I have a web application that receives Unix timestamps from a MySQL database through an API call. I need to display only the time portion (not the date) to users in a readable format like HH:MM:SS.
What’s the best approach to convert these timestamps into time strings using JavaScript? I’ve tried a few methods but I’m not getting the results I expect.
const unixTime = 1672531200;
const timeString = formatTimeFromUnix(unixTime);
console.log(timeString); // Should output something like "14:30:45"
function formatTimeFromUnix(timestamp) {
// Need help implementing this function
return "??:??:??";
}
Any suggestions on how to properly extract and format the time component would be really helpful.
just multiply it by 1000 and then do new Date(yourTimestamp * 1000).toLocaleTimeString(). itll format to or like HH:MM:SS dependin on your locale. this has worked fine for me.
Honestly, the easiest way I’ve found is using toISOString and slicing it. new Date(timestamp * 1000).toISOString().substr(11, 8) gives you exactly HH:MM:SS format without timezone weirdness. Works every time and doesn’t need extra formatting logic.
Been dealing with timestamp conversions in production for years. One thing that bit me early - Unix timestamps sometimes come as strings from APIs, which breaks the multiplication. Always parse first to avoid silent failures.
function formatTimeFromUnix(timestamp) {
const numericTimestamp = parseInt(timestamp);
const date = new Date(numericTimestamp * 1000);
return date.toTimeString().slice(0, 8);
}
toTimeString() returns “14:30:45 GMT-0800 (PST)” so slicing the first 8 characters gives you exactly what you need. Handles formatting automatically while staying lightweight. Way more reliable than custom padding logic, especially with edge cases like daylight saving transitions.
Yeah, multiplying by 1000 is right since JavaScript wants milliseconds, but watch out for timezone issues depending on your users’ locations. I’ve hit similar problems converting timestamps from different databases.
function formatTimeFromUnix(timestamp) {
const date = new Date(timestamp * 1000);
const hours = date.getHours().toString().padStart(2, '0');
const minutes = date.getMinutes().toString().padStart(2, '0');
const seconds = date.getSeconds().toString().padStart(2, '0');
return `${hours}:${minutes}:${seconds}`;
}
This gives you way more control over formatting and skips the locale-specific stuff that can confuse users. padStart makes sure you always get two digits even for single values. Way more reliable than toLocaleTimeString when you need consistent formatting across different environments.
The Date approach works but gets messy when you’re converting timestamps regularly across your app. I’ve hit this exact issue building dashboards that pull timestamp data from multiple APIs.
Here’s a clean solution:
function formatTimeFromUnix(timestamp) {
const date = new Date(timestamp * 1000);
return date.toLocaleTimeString('en-US', {
hour12: false,
hour: '2-digit',
minute: '2-digit',
second: '2-digit'
});
}
But if you’re doing lots of timestamp conversions or need different formats, automate the whole thing. I’ve built systems where timestamp formatting happens automatically as data flows from database to frontend.
Latenode makes this dead simple. Create a workflow that catches your API responses, formats all timestamps automatically, and sends clean data to your frontend. No more manual conversions scattered everywhere.
It handles formatting logic in one spot and applies it consistently across your app. Way cleaner than writing formatting functions all over the place.