moment().fromNow() is returning "a few seconds ago".
Why not "just now" ?
You can customize that by doing something like moment.langData('en').relativeTime.s = "just now", although it would look weird with a suffix.
If you want, you can create a method that wraps fromNow.
moment.fn.fromNowOrNow = function (a) {
if (Math.abs(moment().diff(this)) < 1000) { // 1000 milliseconds
return 'just now';
}
return this.fromNow(a);
}
Thanks Tim!
Thanks for the code. I'm fine with "a few seconds ago", but I often also get "in a few seconds" after completing an operation because the server and client clocks aren't in sync. So I wonder if it wouldn't make sense to show "just now" in both cases?
Yeah, the code above shows "just now" if its 1 second before or after the current time, so if you want to increase that range, you can just change the number of milliseconds.
moment.fn.fromNowOrNow = function (a) {
if (Math.abs(moment().diff(this)) < 25000) { // 25 seconds before or after now
return 'just now';
}
return this.fromNow(a);
}
Thanks, I got that, but was wondering if it would make sense for 'just now' to be the default behavior, given that seeing "in a few seconds" for a past event looks like a bug to most users.
+1 for 'just now' to be the default behavior!
Well, if you clocks are out of sync you have an issue. I don't think the right place to fix it is moment's humanize method. Its a time library, you tell it to display a time in the future, and it does so.
If you want to change the string "a few seconds ago" with "just now", but leave "in a few seconds" that is a different thing. It will also require a fix of all translations ;-)
moment.langData('en').relativeTime.s = "never"; was the correction for my issue #555
Thank You Tim!
The clocks are almost always going to be a bit out of sync when parsing server-generated timestamps in a browser. Is this a rare use case for moment.js?
I think adding an epsilon value of a few seconds, so that intervals smaller than that in any direction would produce "just now" or similar makes sense, and if this value is easily changeable it can fit everybody's expectations -- value 0 means current behavior, value greater than zero is to fight browser/server offset or similar.
What do you think?
A configurable "epsilon value" sounds like a good idea.
I think a simpler solution to @ejain's issue is to square the moment up before displaying it. You should usually know if it's actually possible for the operation to have happend in the future.
var now = moment();
var mom = momentFromServer < now ? now : momentFromServer;
mom.fromNow(); //=> a few seconds ago
If this needs to be part of Moment, my suggestion is just to make it a boolean argument to fromNow(). Or better yet, let fromNow take a (reverse-compatible) option object, like fromNow({withSuffix: false, forcePast: true}).
@icambron: great solution, if you can be sure to not have valid future moments.
@timrwood thanks for your tips
it's actually moment.langData('en')._relativeTime.s = "just now" if you want to replace said string.
I am humanizing dates (without time information), so changing _relativeTime.s to the equivalent of "today" for all locales was my solution.
fromNow(true) returns "2 minutes ago","2 hours ago","a few seconds ago".
How can you do something like "2m" or "2h" or "2s" or "2d"?.
@puneet94 You can update your locale strings with https://momentjs.com/docs/#/customization/relative-time/
FWIW, I was able to apply "just now" using 2.12.0 by specifying a function for past:
moment.updateLocale('en', {
relativeTime : {
past: function(input) {
return input === 'just now'
? input
: input + ' ago'
},
s : 'just now',
future: "in %s",
ss : '%d seconds',
m: "a minute",
mm: "%d 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"
}
});
@richardszalay what date format are you using with this and how do you apply this?
I have a date string time = 1560955417474,
and want to display it within a text tag
<Text>{time}</Text>
Most helpful comment
You can customize that by doing something like
moment.langData('en').relativeTime.s = "just now", although it would look weird with a suffix.If you want, you can create a method that wraps
fromNow.