Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ngoquocdat's avatar

How to convert time in js

I have time: 1614358672 How do I convert it to this kind of time: 2/26/2021, 11:57:52 PM

this is my code but it not working

let date = new Date(1614358672)
console.log(date.toLocaleDateString() + ', ' + date.toLocaleTimeString())
0 likes
5 replies
tykus's avatar

You are missing the locale. Just use toLocaleString("en-US") as this will have both the date and time parts

ngoquocdat's avatar
let date = new Date(1614358672)
console.log(date.toLocaleString('en-US'))
// 1/20/1970, 12:25:58 AM

it not working

tykus's avatar
tykus
Best Answer
Level 104

Sorry, I was focused on the formatting and didn't notice how you instantiated the Date. You must multiply the Unix timestamp by 1000 because in Javascript the timestamps are in milli-seconds

let date = new Date(1614358672 * 1000)
console.log(date.toLocaleString('en-US', { timeZone: 'America/New_York' }))

Edit pass the required timezone in the second argument

1 like
trin's avatar

use dayjs and be happy. his save your time.

const time = dayjs(1614358672000).tz("America/New_York").format('M/D/YYYY, h:mm:ss A')

Please or to participate in this conversation.