更新时间:2023-02-23 17:07:13
你可以使用 updateLocale $自定义时刻为您的区域设置格式化相对时间的方式c $ c>
。
You can customize how moment formats relative time for you locale using updateLocale
.
请注意,文档说:
如果语言环境需要对令牌进行额外处理,则可以将令牌设置为具有以下签名的函数。该函数应返回一个字符串。
If a locale requires additional processing for a token, it can set the token as a function with the following signature. The function should return a string.
function (number, withoutSuffix, key, isFuture) {
return string;
}
在您的情况下,您可以这样做:
In your case, you can do something like this:
var m1 = moment().subtract(5, 'm');
var m2 = moment().subtract(15, 's');
console.log(m1.fromNow());
console.log(m2.fromNow());
moment.updateLocale('en', {
relativeTime : {
future: "in %s",
past: "%s ago",
s: function (number, withoutSuffix, key, isFuture){
return '00:' + (number<10 ? '0':'') + number + ' minutes';
},
m: "01:00 minutes",
mm: function (number, withoutSuffix, key, isFuture){
return (number<10 ? '0':'') + number + ':00' + ' minutes';
},
h: "an hour",
hh: "%d hours",
d: "a day",
dd: "%d days",
M: "a month",
MM: "%d months",
y: "a year",
yy: "%d years"
}
});
console.log(m1.fromNow());
console.log(m2.fromNow());
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.14.1/moment.min.js"></script>
我不确定上面的代码涵盖了你需要的所有情况,但我认为它可以是一个很好的起点。
I'm not sure that the code above covers all the case you need, but I think that it can be a good starting point.