Get hours, minutes and seconds from a timestamp or a date time string

Get hours, minutes and seconds from timestamp or date time string in Javascript:

The first, we have two types of date time data bellow:

 const dateTimestamp = 1586933584; // timestamp
 const dateTimeString = '2020-04-15 15:48:53'; // datetime string

To convert it into a time string like this:

We use this way:

 const dateObject = new Date(dateTimestamp); // Convert to a date time object first
 // const dateObject = new Date(dateTimeString); // for date time string

 const hours = dateObject.getHours(); // get hours with getHours method
 const minutes = dateObject.getMinutes(); // get minutes with getMinutes method
 const seconds = dateObject.getSeconds(); // get seconds with getSeconds method

 const timeString = `${hours}h : ${minutes}m : ${seconds}s`; // finally, join to a time string
 console.log(timeString) // 15h : 48m : 53s

Read more: Convert seconds to time value (Hours : Minutes : Seconds )

© 2019 4codev

Created with love by Sil.