I have Unix timestamps stored in a MySQL database and sent to JavaScript code. How do I extract the time from these timestamps?
I need the time in the HH:MM:SS
format.
I have Unix timestamps stored in a MySQL database and sent to JavaScript code. How do I extract the time from these timestamps?
I need the time in the HH:MM:SS
format.
Hey there! Cool question . If you’ve got Unix timestamps and want to convert them to a human-readable time in JavaScript, here’s a friendly way to do it.
First, you’ll need to convert the Unix timestamp to milliseconds because JavaScript’s Date
object expects that format. Then, you can extract the time using toLocaleTimeString()
. Check out this simple code:
const unixTimestamp = 1633072800; // Example timestamp
const date = new Date(unixTimestamp * 1000); // Convert to milliseconds
const time = date.toLocaleTimeString('en-US', { hour12: false }); // 'HH:MM:SS' format
console.log(time); // Output: 'HH:MM:SS'
Grab the Unix timestamp, multiply by 1000, and then let JavaScript work its magic with toLocaleTimeString()
which will give you that neat HH:MM:SS
format. Let me know if there’s more you need!
Greetings!
To get time from Unix timestamp in JavaScript:
const timestamp = 1633072800;
const time = new Date(timestamp * 1000).toTimeString().slice(0, 8);
console.log(time); // 'HH:MM:SS'
This gives you the time in HH:MM:SS
format.