Reputation: 10219
I'm using the Time to X function of Moment.js. When the second parameter is true
, then the result is a day
. Is there a way to make this 1 day
through the API, or will I need to do a string replace?
Upvotes: 1
Views: 295
Reputation: 515
I've got a simple 100% solution to your question:
moment.localeData()._relativeTime.d = '1 day'; // override
console.log( moment.duration(1, 'days') ); // = '1 day', not 'a day'.
here's a fiddle with comments: https://jsfiddle.net/ichepurnoy/6bxujv8j/
Upvotes: 0
Reputation: 172448
You can use the string.replace method to replace "a day"
with "1 day"
.
Or you can try like this:
var a = moment([2007, 0, 29]);
var b = moment([2007, 0, 28]);
var differenceInMillisec = a.diff(b);
var differenceInDays = a.diff(b, 'days'); //Output 1 day
Upvotes: 2
Reputation: 249
well, sometimes when the result is very simple, just go for it. a string replace is just one line of code... Even if there is a solution with the api, I'm sure it would be more "complicated" than doing a string replace... or just write a reusable function and call that function on the result like : replaceString(Time_to_X); where replaceString is a function using a string replace
Upvotes: 1