[英]Use “5 days ago (Tue)” if within the last week in moment.js
我正在使用moment.js。
相對過去幾天的默認值是"5 days ago"
。 但我想要的是,如果它在一周前它應該返回"5 days ago (Tue)"
。 如果超過一周,我想要常規的"5 days ago"
。
文檔說我可以提供一個函數來自定義格式這樣的東西:
moment.locale('en', {
relativeTime : {
future: "in %s",
past: "%s ago",
s: "seconds",
m: "a minute",
mm: "%d minutes",
h: "an hour",
hh: "%d hours",
//d: "a day", // this is the default
d: function(num, noSuffix, key, future) { return "a day (" + FOO + ")"; },
//dd: "%d days", // this is the default
dd: function(num, noSuffix, key, future) { return num + "days (" + FOO + ")"; },
M: "a month",
MM: "%d months",
y: "a year",
yy: "%d years"
}
});
問題是:
FOO
的工作日名稱? 5 days (Mon) ago
而不是5 days ago (Mon)
您無法以您提出的方式操縱相對時間格式。 但是,您可以自己進行比較,以決定是否附加其他字符串。
// your source moment
var m = moment("2015-06-04");
// calculate the number of whole days difference
var d = moment().diff(m,'days');
// create the output string
var s = m.fromNow() + (d >= 1 && d <= 7 ? m.format(" (ddd)") : "");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.