I\'m running into a small formatting issue with moment\'s a
input.
a/A will return AM/am PM/pm but is there a way to format this to include periods?
<Based on @user4040648 answer here is how you can implement it in es6
moment.updateLocale('en', {
meridiem(hour, minute, isLowerCase) {
return hour < 12 ? 'a.m.' : 'p.m.';
}
});
Following moment docs you can have the following:
moment.updateLocale('en', {
meridiem: function (hour, minute, isLowercase) {
if( hour >= 12 )
return isLowercase ? 'p.m.' : 'P.M.';
else
return isLowercase ? 'a.m.' : 'A.M.';
}
});
console.log(moment().hour(1).format('HH:mm a'));
console.log(moment().hour(15).format('HH:mm A'));
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.13.0/moment.min.js"></script>
As stated in the documentation, from moment version 1.6.0
Locale#meridiem
should be a callback function that returns the correct string based on hour, minute, and upper/lowercase.
While on version before 1.6.0:
Locale#meridiem
was a map of upper and lowercase versions of am/pm.
The meridiem map was in versions 1.5.x and below, after 1.6.0 the meridiem is a function that returns the value (see link).
The method you used to customize the meridiem applies to versions < 1.6.0. You should provide a function in newer versions to update meridiem. Please see the docs for more info:
moment.updateLocale('en', {
meridiem: function(hour, minute, isLowerCase) {
if (hour < 12) {
return 'a.m.';
} else {
return 'p.m.';
}
}
});