gsk
gsk

Reputation: 2379

Get Diffrence between two date string in momentjs?

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

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

How it is possible here?

Upvotes: 2

Views: 3105

Answers (3)

Kalhan.Toress
Kalhan.Toress

Reputation: 21901

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

Upvotes: 3

ozil
ozil

Reputation: 7117

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");

Upvotes: 0

Satpal
Satpal

Reputation: 133403

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();

Upvotes: 2

Related Questions