Moment.js 自定义 AM/PM
在本文中,我们将学习如何在 Moment.js 中自定义 AM/PM 逻辑。可以使用 meridiem 回调函数根据区域设置自定义上午/下午。它根据回调函数的逻辑返回 AM/PM 字符串。
语法:
moment.updateLocale('en', {
meridiem: Function
});
以下示例将帮助演示在Moment.js中自定义AM/PM。
示例1:
const moment = require('moment');
moment.updateLocale('en', {
// Specify the callback function for
// customizing the values
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(13).format('HH:mm A'));
输出:
13:05 P.M.
示例2:
const moment = require('moment');
let localeData = moment.updateLocale('fr', {
meridiem: function (hours, minutes, isLower) {
return hours < 12 ? 'PD' : 'MD';
}
});
var m = moment('2022-08-15T20:00:00').format('hh a');
console.log("Customized AM/PM is :", m);
输出:
Customized AM/PM is : 08 MD
参考: https://momentjs.com/docs/#/customization/am-pm/