2

我正在使用momentjs作为日期,并且我有一个日期字符串,即,"2015-05-10"我想获取与今天的日期差异

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

这里怎么可能?

4

3 回答 3

3

这是一个例子,

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

在此处查看更多选项

这是一个例子

于 2015-05-13T11:14:53.623 回答
2

您可以使用差异

//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();
于 2015-05-13T11:14:40.523 回答
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");
于 2015-05-13T11:18:33.877 回答