Reputation: 3471
I have the following code, which is used to find the humanized time difference between two dates, the problem is that I don't want to have 'a day ago', but show 'yesterday'.
function humanize(date) {
const then = moment.utc(date);
return then.fromNow();
}
function getYesterdayAsISO() {
var date = new Date();
date.setDate(date.getDate()-1);
return date.toISOString();
}
console.log(humanize(getYesterdayAsISO()));
<script src="https://momentjs.com/downloads/moment-with-locales.min.js"></script>
Upvotes: 3
Views: 643
Reputation: 179
moment.updateLocale("en-gb", {
relativeTime: {
future: "in %s",
past: "%s",
s: "a few seconds ago",
ss: "%d seconds ago",
m: "a minute ago",
mm: "%d minutes ago",
h: "an hour ago",
hh: "%d hours ago",
d: "yesterday",
dd: "%d days ago",
M: "a month ago",
MM: "%d months ago",
y: "a year ago",
yy: "%d years ago",
},
});
this is stupid code but it resolved my issue
Upvotes: 1
Reputation: 167182
These are found in translation files. You can change them in the relativeTime
in en-gb.js
at Line 42:
d : 'yesterday',
But this still shows as yesterday ago
. The right one you can change is the past
past : '%s ago',
Also, without changing the source, I added the following by injecting:
moment.updateLocale('en-gb', {
relativeTime: {
past : '%s',
d : 'yesterday'
}
});
The above may not work in all the cases, and it should be specifically used only for your current project.
I get the following in the console:
Working Example
moment.updateLocale('en-gb', {
relativeTime: {
past : '%s',
d : 'yesterday'
}
});
function humanize(date) {
const then = moment.utc(date);
return then.fromNow();
}
function getYesterdayAsISO() {
var date = new Date();
date.setDate(date.getDate()-1);
return date.toISOString();
}
console.log(humanize(getYesterdayAsISO()));
<script src="https://momentjs.com/downloads/moment-with-locales.min.js"></script>
Upvotes: 4