I'm using moment.js.
The default for relative past days is "5 days ago"
. But what I want is that if it's within a week ago it should return "5 days ago (Tue)"
. If it's more than a week, I want the regular "5 days ago"
.
The docs say I can supply a function to custom format such a thing:
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"
}
});
The problems are:
FOO
?5 days (Mon) ago
instead of 5 days ago (Mon)
You can't manipulate the relative time format in the way you asked. However, you can simply do the comparison yourself to decide whether or not to append the additional string.
// 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)") : "");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With