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.