Mārtiņš Radiņš
Mārtiņš Radiņš

Reputation: 375

Display timepicker in correct locale format

I am using Bootstrap daterangepicker. Moment JS is being used to set the locale for date formatting etc.

I would like to to show the timepicker based on the Moment JS locale. Daterangepicker has a parameter for setting the picker format (12h/24h) at the initialization

timePicker24Hour: true (or false/no parameter for 12h format)

How could I get locale information regarding the time format from Moment JS? I could then set the proper timepicker type on init.

Upvotes: 0

Views: 603

Answers (1)

VincenzoC
VincenzoC

Reputation: 31482

You can use moment.localeData and the longDateFormat method to get locale specific information about time format. Then you can test if the format contains the HH (24 hours) token.

Here a live sample:

function is24Hour(locale){
  var localeData = moment.localeData(locale);
  var hourFormat = localeData.longDateFormat('LT');
  return hourFormat.includes('HH');
}

console.log( is24Hour('en') );
console.log( is24Hour('it') );
console.log( is24Hour('fr') );
console.log( is24Hour('de') );
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment-with-locales.min.js"></script>

This may need further tests for other locales.

Upvotes: 1

Related Questions