valera
valera

Reputation: 79

Momentjs seconds from now

var startdate  =  '02.05.2018 18:05:03';

How to find out how many minutes have passed from startdate to now?

My try

var exp = moment(startdate);
minutes = moment().diff(exp, 'minutes');

but result 124764 it not right

Upvotes: 4

Views: 10366

Answers (2)

Tuğca Eker
Tuğca Eker

Reputation: 1493

To get the difference between two moment time objects, you can use .diff(). Then you can use any of asHours(), asMinutes(), asSeconds() to get human-readable time difference.

var start  =  moment('02.05.2018 18:05:03');
var end  =  moment('02.05.2018 18:11:03');

var duration = moment.duration(end.diff(start));
var mins = duration.asMinutes();

console.log(mins)

In your case, you can simply call moment().diff(time) since you want to get difference between the time you specify and now.

var time  =  moment('02.05.2018 18:05:03');

var duration = moment.duration(moment().diff(time));
var mins = duration.asMinutes();

console.log(mins)

Upvotes: 1

31piy
31piy

Reputation: 23859

Parsing of date strings are not consistent among browsers. Always pass the format of the string if it is in non-ISO format to prevent unwanted bugs:

var exp = moment(startdate, 'DD.MM.YYYY HH:mm:ss');
moment().diff(exp, 'minutes');

Upvotes: 6

Related Questions