Time (hour, minute) format localization in moment.js

SVMJ

As momentjs has the ability take into account the date format for differernt locales, does it have the ability to localize time (hour:minute) format?

According to oracle docs time format there're some time format difference across region https://docs.oracle.com/cd/E19455-01/806-0169/overview-6/index.html

moment().locale(somelocale).format('L') this will output the formatted date

However

moment().locale(somelocale).format('LT') doesn't seem like to have the same ability?

class TimeFormatter extends React.Component {
  render() {
    const browserLocale = window.navigator.userLanguage || window.navigator.language;
    return (
      <div>
        {moment(this.props.value, 'HH:mm').locale(browserLocale).format('LT')}
      </div>);
  }
}

Above is my code for formatting the time cell on react data grid, but while I change the chrome settings the format stays the same.

So if I want to achieve time format localization can I use momentjs or I have to just come up a if else condition for setup specific user region time format

Many Thanks

Matt Johnson-Pint

The LT format does take locale into account.

Here are some examples:

moment().locale('en-CA').format('LT') //=> "1:21 PM"
moment().locale('fr-CA').format('LT') //=> "13:21"
moment().locale('fi-FI').format('LT') //=> "13.21"
moment().locale('de-DE').format('LT') //=> "13:21"
moment().locale('no-NO').format('LT') //=> "1:21 PM"
moment().locale('th-TH').format('LT') //=> "13:21"
moment().locale('en-GB').format('LT') //=> "13:21"

Note that the Oracle page you linked to is part of the Solaris 8 documentation - an operating system which came out in February 2000 and reached end of life in March 2012. It is sorely mistaken with regard to the time formats used in those countries and languages. You should not use it as a reference.

Also note that that it made a grave error in assuming that "Canadian" was enough to identify a locale completely. As shown above, French speaking Canadians use a 24-hour clock, but many English speaking Canadians sometimes use a 12-hour clock (see Time notation in Canada) and thus en-CA gives the 12-hour format while fr-CA gives the 24-hour format.

Thus, country alone is not enough. A locale must consist of at least language, but usually both language and country are necessary. These are now called "IETF language tags" and are standardized by BCP 47.

As far as why Moment's German localization doesn't include the string Uhr in the result, that was something that was first added with #1601, but then removed with #2006 - both back in 2014. See those issues for reasoning.

Also note that Moment's localization strings come from community submitted feedback and moment contributors. In many cases they align to the standards collated by Unicode CLDR, but in some cases they differ. If you are looking for a modern date library with standardized localization support, consider Luxon which leverages the internationalization APIs built into modern browsers.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Turn a String to a 24 hour time format, using moment.Js

AngularJS datetime format get wrong hour and minute value based on localization

Add 15 minute to 12 hour time format

Match hour:minute time format with regex in Python

Moment.js 24-hour time format, dealing with the 24th hour

How to get current moment in ISO 8601 format with date, hour, and minute?

Moment.js display hour in local format

Is there a way to check if a variable is a time in hour : minute time zone format?

Moment.js time to format

Convert hour UTC time to local time moment.js

Moment.js: how to update the time every minute?

Round moment.js object time to nearest 30 minute interval

moment js returning 'invalid date ' in case of 24 hour format

Format time in Chat Application using Moment JS

Moment.js decimals into time format

Javascript - Format date/time with moment.js

moment js `format` method breaks time

Moment JS format() not displaying correct time

Get date time in specific format in moment js

Convert Time Stamp into date, time, hour, minute

Format time interval in seconds as X hour(s) Y minute(s) Z second(s)

How to calculate time difference using <hour>h<minute> format Java Netbeans

How to convert text like "1 day(s), 14 hour(s), 16 minute(s)" to time format?

Does anyone know how to get the time to format correctly when the Hour/Minute/Second is a SINGLE digit in python or pandas

How to show time (Hour and minute) and add the time for 30 minute

Display current time- hour and minute in angular

Get time with hour, minute, second, millisecond, microsecond

Pandas combine Hour and Minute column to time

Casting a specific hour and minute and getting the time difference