Search code examples
javascriptmomentjsmoment-timezone

Localized short time string using moment.js


How do I get time string from moment.js object with respect to current locale?

moment().format('HH:mm') 

always get the same result regardless of localization.

I want similar result as I would use in angular using shortTime:

formatDate(new Date(), 'shortTime'): // HH:mm, resp hh:mm a

Solution

  • You can simply use Localized formats listed in format() docs

    Because preferred formatting differs based on locale, there are a few tokens that can be used to format a moment based on its locale.

    There are upper and lower case variations on the same formats. The lowercase version is intended to be the shortened version of its uppercase counterpart.

    Here a live sample:

    console.log( moment().format('LT') );
    moment.locale('it');
    console.log( moment().format('LT') );
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment-with-locales.min.js"></script>