Time to X2.10.3+
moment().to(Moment|String|Number|Date|Array);
moment().to(Moment|String|Number|Date|Array, Boolean);
You may want to display a moment in relation to a time other than now. In that case, you can use moment#to
.
var a = moment([2007, 0, 28]);
var b = moment([2007, 0, 29]);
a.to(b) // "in a day"
The first parameter is anything you can pass to moment()
or an actual Moment
.
var a = moment([2007, 0, 28]);
var b = moment([2007, 0, 29]);
a.to(b); // "in a day"
a.to([2007, 0, 29]); // "in a day"
a.to(new Date(2007, 0, 29)); // "in a day"
a.to("2007-01-29"); // "in a day"
Like moment#toNow
, passing true
as the second parameter returns value without the suffix. This is useful wherever you need to have a human readable length of time.
var start = moment([2007, 0, 5]);
var end = moment([2007, 0, 10]);
end.to(start); // "5 days ago"
end.to(start, true); // "5 days"
From version 2.10.3, if any of the endpoints are invalid the result is thelocalized Invalid date string.
当前内容版权归 momentjs.com 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 momentjs.com .