npm install moment-precise-range-plugin
由 Rob Dawson 编写的 Precise Range 插件可用于显示日期/时间范围的准确的人类可读的表示形式:
moment("2014-01-01 12:00:00").preciseDiff("2015-03-04 16:05:06");
// 1 year 2 months 3 days 4 hours 5 minutes 6 seconds
moment.preciseDiff("2014-01-01 12:00:00", "2014-04-20 12:00:00");
// 3 months 19 days
要获取原始数字值而不是字符串,则将 true
值作为第三个参数传给该方法:
moment.preciseDiff(m1, m2, true);
// {years : 0, months : 1, days : 2, hours : 3, minutes : 4, seconds : 5, firstDateWasLater : false}
npm install moment-precise-range-plugin
The Precise Range plugin, written by Rob Dawson, can be used to display exact, human-readable representations of date/time ranges:
moment("2014-01-01 12:00:00").preciseDiff("2015-03-04 16:05:06");
// 1 year 2 months 3 days 4 hours 5 minutes 6 seconds
moment.preciseDiff("2014-01-01 12:00:00", "2014-04-20 12:00:00");
// 3 months 19 days
To obtain the raw numeric values rather than a string, pass the value true
as the third argument to the method:
moment.preciseDiff(m1, m2, true);
// {years : 0, months : 1, days : 2, hours : 3, minutes : 4, seconds : 5, firstDateWasLater : false}