DEV Community

Gaute Meek Olsen
Gaute Meek Olsen

Posted on • Edited on • Originally published at gaute.dev

3 2

Formated clock in JavaScript

How to get a formatted clock display easily in JavaScript with Intl.DateTimeFormat.

const date = new Date();
const time = new Intl.DateTimeFormat('default', {
  hour: 'numeric',
  minute: 'numeric',
}).format(date)
Enter fullscreen mode Exit fullscreen mode

Bonus, it will be formatted according to the users' local settings. For me, time will be '23:00', while those on 12-hour clock will see '11:00 PM'.

No need to deal with string splitting, or concatenation of values.

Top comments (1)

Collapse
 
khuongduybui profile image

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →