在momentjs中获取两个日期字符串之间的差异?

时间:2015-05-13 11:10:51

标签: javascript jquery angularjs momentjs date-difference

我正在使用momentjs作为日期,我有一个日期字符串,即"2015-05-10",我希望从今天获得日期差异

 var today= moment().format('YYYY-MM-DD');

这里怎么可能?

3 个答案:

答案 0 :(得分:2)

您可以使用diff

//Convert to date 
var today = moment();
var date = moment("2015-05-13", "YYYY-MM-DD");

//Use diff
var duration = today.diff(date);
var hours = duration.asHours();

答案 1 :(得分:2)

这是一个例子,

var now = moment(); // moment object of now
var day = moment("2015-05-13"); // moment object of other date

$scope.difference = now.diff(day, 'days'); // calculate the difference in days

$scope.difference = now.diff(day, 'hours'); // calculate the difference in hours

查看更多选项here

这是一个example

答案 2 :(得分:0)

如果你在谈论time difference in hours

var now  = "04/09/2013 15:00:00";
var then = "02/09/2013 14:20:30";

var ms = moment(now,"DD/MM/YYYY HH:mm:ss").diff(moment(then,"DD/MM/YYYY HH:mm:ss"));
var d = moment.duration(ms);
var s = Math.floor(d.asHours()) + moment.utc(ms).format(":mm:ss");