Hoài Vũ Lê
Hoài Vũ Lê

Reputation: 75

Javascript get time in unix from a unix timestamp

How can I extract time only as a unix number from a unix timestamp? For example:

const timeStamp = 1671682809 // Thursday, December 22, 2022 11:20:09 AM GMT+07:00
const unixTimeOnly = extractTime(timeStamp) // return a unix number that represents "11:20:09"

Thank you!

Upvotes: 0

Views: 92

Answers (1)

Ryan_DS
Ryan_DS

Reputation: 812

You could do this by subtracting the same date, with the clock set to midnight:

const timeStamp = 1671682809;
const date = new Date(timeStamp * 1000);
const midnightDate = new Date(date).setHours(0,0,0,0);
const justHMS = date - midnightDate;
// you may want to divide this number how you wish if you're not working in milliseconds
console.log(justHMS); 

Upvotes: 1

Related Questions