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

时间:2021-06-24 01:53:34

I am using momentjs for date and I have one date string, ie,"2015-05-10",I want to get date difference from today

我使用momentjs作为日期,我有一个日期字符串,即“2015-05-10”,我想从今天获得日期差异

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

How it is possible here?

怎么可能在这里?

3 个解决方案

#1


here is a example,

这是一个例子,

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

check more options here

在这里查看更多选项

here is a example

这是一个例子

#2


You can use 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();

#3


if you are talking about 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");

#1


here is a example,

这是一个例子,

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

check more options here

在这里查看更多选项

here is a example

这是一个例子

#2


You can use 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();

#3


if you are talking about 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");