DEV Community

Cover image for Format Current Time πŸ•°οΈ
Manav Misra
Manav Misra

Posted on

Format Current Time πŸ•°οΈ

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".

Top comments (3)

Collapse
 
konradlinkowski profile image
Konrad Linkowski

You should rather use Intl instead

Collapse
 
codefinity profile image
Manav Misra

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.