Moment.js display time in local format
I think this is a pretty simple question, but I cannot find it in the documentation at this time. I have to display only hour in local format eg.
- for locale
en-gb
: 14 - for locale
en-us
: 2 PM
The closest format I have found is LT
, but it also displays minutes that are not needed ...
Here's what I've tried:
moment().format('LT'); // 14:00 / 2:00 PM
// I need some kind of combination of these two:
moment().format('H'); // 14
moment().format('h A'); // 2 PM
Is there some point to achieve it or am I forced to use a workaround like below?
moment().format('LT').replace(/:[0-9]{2}/, '');
source to share
There is no built-in method in momentjs 2.18.1 to get the localized input you are looking for. localized formats that include time:
Time | LT | 8:30 PM
Time with seconds | LTS | 8:30:25 PM
Month name, day of month, day of week, year, time | LLLL | Thursday, September 4 1986 8:30 PM
| llll | Thu, Sep 4 1986 8:30 PM
Thus, you will probably have to use the work as you showed in the question. The following script creates and displays a map with the number of occurrences for each line LT
. He uses localeData
and longDateFormat
to produce a localized format for LT
.
let formatMap = new Map();
['af' , 'ar-dz', 'ar-kw', 'ar-ly', 'ar-ma', 'ar-sa', 'ar-tn', 'ar', 'az', 'be', 'bg', 'bn', 'bo', 'br', 'bs', 'ca', 'cs', 'cv', 'cy', 'da', 'de-at', 'de-ch', 'de', 'dv', 'el', 'en-au', 'en-ca', 'en-gb', 'en-ie', 'en-nz', 'eo', 'es-do', 'es', 'et', 'eu', 'fa', 'fi', 'fo', 'fr-ca', 'fr-ch', 'fr', 'fy', 'gd', 'gl', 'gom-latn', 'he', 'hi', 'hr', 'hu', 'hy-am', 'id', 'is', 'it', 'ja', 'jv', 'ka', 'kk', 'km', 'kn', 'ko', 'ky', 'lb', 'lo', 'lt', 'lv', 'me', 'mi', 'mk', 'ml', 'mr', 'ms-my', 'ms', 'my', 'nb', 'ne', 'nl-be', 'nl', 'nn', 'pa-in', 'pl', 'pt-br', 'pt', 'ro', 'ru', 'sd', 'se', 'si', 'sk', 'sl', 'sq', 'sr-cyrl', 'sr', 'ss', 'sv', 'sw', 'ta', 'te', 'tet', 'th', 'tl-ph', 'tlh', 'tr', 'tzl', 'tzm-latn', 'tzm', 'uk', 'ur', 'uz-latn', 'uz', 'vi', 'x-pseudo', 'yo', 'zh-cn', 'zh-hk', 'zh-tw'].forEach(localeName => {
var format = moment.localeData(localeName).longDateFormat('LT');
if( formatMap.has(format) ){
let val = formatMap.get(format);
formatMap.set(format, val+1);
} else {
formatMap.set(format, 1);
}
});
console.log(formatMap);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment-with-locales.min.js"></script>
Note that the breton ( br
) language uses the format h[e]mm A
, and there are 7 locales (including: German (Switzerland) de-ch
, Finnish fi
, Indonesian id
, etc.) that use HH.mm
.
These cases are not covered by your workaround, perhaps you can use something like:
moment().format('LT').replace(/(:|\.)[0-9]{2}/, '');
or
moment().format('LT').replace(/(:|\.|e)[0-9]{2}/, '');
to get the desired result for those locales.
source to share
You can use moment.locale API to create your custom format http://momentjs.com/docs/#/customization/long-date-formats/
source to share
Just try moment().format('hh');
returning the clock in binary, for example 09.10. If you want to use one digit moment().format('h');
, this will only print 2,3, ... When you want to print hours using the meridium moment().format('hh a');
and moment().format('h a');
both of these are printed along the meridians in small letters. You can also use moment().format('hh a');
it moment().format('h a');
to represent the meridian in capital letters. This way you can print the clock in local format. Hope this helps you.
source to share
What you are trying to achieve can be done with the local.js local API as Rahmat Aligos .
Looking at their website functionality, this is a piece of code that will do the required actions.
$(document).on('click', '[data-locale]', function() {
var dom = $(this);
currentLang = dom.data('locale');
$('[data-locale]').removeClass('active');
dom.addClass('active');
updateSnippets();
});
function updateSnippets() {
var i;
moment.locale(currentLang); // si or en-gb or tzm-latn etc..
for (i = 0; i < snippets.length; i++) {
snippets[i].update();
}
}
Note that they set the selected country in moment.locale (currentLang). Example: if you choose Arabic (Kuwait) then the output will be:
moment (). format ('MMMM Do YYYY, h: mm: ss a'); // غشت 9 2017, 9:31:00 am
If you want to define the browser language for setting local.js local use the following code:
var language = window.navigator.userLanguage || window.navigator.language;
console.log(language); //works IE/SAFARI/CHROME/FF
source to share