DEV Community

loading...
Cover image for Format Current Time ๐Ÿ•ฐ๏ธ

Format Current Time ๐Ÿ•ฐ๏ธ

codefinity profile image Manav Misra ใƒป1 min read
new Date(Date.now()).toLocaleTimeString("en-US", {
  timeZone: "America/Chicago",
    })
Enter fullscreen mode Exit fullscreen mode

The above code outputs something like: 4:30:05 PM.

For me, this comes in handy when running a Node server that is being watched by something like nodemon. In this way, I can see ๐Ÿ‘€ the timings and know that things are updating properly as I save my code.

Note that if you want the entire date included, just use: toLocaleString instead of toLocaleTimeString ๐Ÿ‘†๐Ÿพ.

Finally, you can see ๐Ÿ‘€ the list here to get the correct TZ strings such as "America/Chicago".

Discussion (3)

pic
Editor guide
Collapse
konradlinkowski profile image
Konrad Linkowski

You should rather use Intl instead

Collapse
codefinity profile image
Manav Misra Author

That works too...not really sure what would be the benefit.

Collapse
konradlinkowski profile image
Konrad Linkowski

developer.mozilla.org/en-US/docs/W...
Intl is the new optimized API where Date objects starts to be a little obsolete.