DEV Community

Cover image for Format Current Time 🕰️
Manav Misra
Manav Misra

Posted on

1

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.

SurveyJS custom survey software

JavaScript UI Libraries for Surveys and Forms

SurveyJS lets you build a JSON-based form management system that integrates with any backend, giving you full control over your data and no user limits. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay