Change Time format to 24 Hours in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 6, 2024
4 min

banner

# Table of Contents

  1. Change Time format to 24 Hours in JavaScript
  2. Change getHours() to 12 Hour Format

Note: If you need to change the getHours() method to 12-hour format, click on the second subheading.

# Change Time format to 24 Hours in JavaScript

Use the toLocaleString() method to change time formatting to 24 hours.

The toLocaleString method returns a string that represents the date and time according to the provided locale and options parameters.

index.js
const date = new Date(); // ๐Ÿ‘‡๏ธ 7/25/2023, 16:30:52 console.log( date.toLocaleString('en-US', { hour12: false, }), );

change time format to 24 hours

The code for this article is available on GitHub

The toLocaleString() method returns a locale-specific string, formatted according to the provided parameters.

The two arguments we passed to the toLocaleString() method are:

  1. locales - a string with a BCP 47 language tag or an array of such strings. You can use any of the available locales, e.g. es-MX for Mexico or en-CA for Canada. If you need more information about this parameter, check out the MDN docs.
  2. options - an object where we set the hour12 property. Read more about the options object in the MDN docs.
The hour12 property specifies whether to use 12-hour time (as opposed to 24-hour time).

We set the value for the property to false, to use the 24-hour time format.

We set the locale to en-US in the example. This formats the date and time according to the US locale.

index.js
const date = new Date(); // ๐Ÿ‘‡๏ธ 7/25/2023, 16:32:04 console.log( date.toLocaleString('en-US', { hour12: false, }), );

If you need to format the date and time according to the visitor's default locale, pass an empty array for the locale parameter.

index.js
const date = new Date(); // ๐Ÿ‘‡๏ธ 7/25/2023, 16:32:17 console.log( date.toLocaleString([], { hour12: false, }), );

# Setting different properties and values on the options object

You can use the different properties on the options object of the toLocaleString method to change the formatting of the date and time.

index.js
const date = new Date(); // ๐Ÿ‘‡๏ธ Tuesday, July 25, 2023 at 16:32:39 console.log( date.toLocaleString('en-US', { dateStyle: 'full', timeStyle: 'full', hour12: false, }), );

setting different properties and values on options object

The code for this article is available on GitHub
We set the dateStyle and timeStyle properties in the options object to full to get a more verbose representation of the date and time.

Other possible values for the two properties are: long, medium and short.

index.js
const date = new Date(); // ๐Ÿ‘‡๏ธ Jul 25, 2023, 16:34:06 console.log( date.toLocaleString('en-US', { dateStyle: 'medium', timeStyle: 'medium', hour12: false, }), );

You can view all of the properties and values the options object implements by visiting the MDN docs.

Here is an example that shows the month, day, hours, minutes and seconds as 2-digits, even if their values are less than 10.

index.js
const date = new Date(); // ๐Ÿ‘‡๏ธ 07/25/2023, 16:34:20 console.log( date.toLocaleString('en-US', { hour12: false, year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', second: '2-digit', }), );
By setting the values of the date and time components to 2 digits, we format them consistently, even if they have a value of less than 10.

If that's the case, the values get padded with a leading zero.

You can view all the other properties the options object supports by visiting the MDN docs.

# Change getHours() to 12 Hour Format

To change the getHours() method to a 12 hour format:

  1. Use the modulo % operator to get the remainder of dividing a call to the getHours() method by 12.
  2. If the remainder is greater than zero, return the remainder.
  3. If the remainder is equal to zero, 12 should be returned.
index.js
function formatHoursTo12(date) { return date.getHours() % 12 || 12; } const date1 = new Date('September 24, 2025 15:24:00'); console.log(formatHoursTo12(date1)); // ๐Ÿ‘‰๏ธ 3 const date2 = new Date('September 24, 2025 23:05:00'); console.log(formatHoursTo12(date2)); // ๐Ÿ‘‰๏ธ 11 const date3 = new Date('September 24, 2025 12:13:00'); console.log(formatHoursTo12(date3)); // ๐Ÿ‘‰๏ธ 12

change get hours to 12 hour format

The code for this article is available on GitHub

We used the modulo (%) operator to get the remainder of dividing the hours by 12.

If the hours are fewer than 12, the number of hours is returned.

index.js
console.log(3 % 12); // ๐Ÿ‘‰๏ธ 3 console.log(11 % 12); // ๐Ÿ‘‰๏ธ 11

On the other hand, if the hours are more than 12, we return the remainder to get a 12 hour format.

index.js
console.log(13 % 12); // ๐Ÿ‘‰๏ธ 1 console.log(16 % 12); // ๐Ÿ‘‰๏ธ 4 console.log(22 % 12); // ๐Ÿ‘‰๏ธ 10

The last step is to handle the edge case where the hours are 12 or 24, because there would be no remainder if we divide either number by 12.

index.js
console.log(12 % 12); // ๐Ÿ‘‰๏ธ 0 console.log(24 % 12); // ๐Ÿ‘‰๏ธ 0

We used the logical OR (||) operator, which returns the value to the left if it's truthy, otherwise, it returns the value to the right.

Truthy are all values that are not falsy.

The falsy values in JavaScript are: false, null, undefined, 0, "" (empty string), NaN (not a number).

0 is a falsy, value, so if the remainder of the division is 0, we return the value to the right of the logical OR (||) operator, which is 12.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev