user6680
user6680

Reputation: 139

momentjs won't get difference between two dates

I'm trying to get the difference between now and an end date in minutes. I have two date variables, but when i try to subtract them, it says Property 'diff' does not exist on type 'string'.

console.log(now) outputs: November 24th 2019, 11:13:32

console.log(endDate) outputs: November 24th 2019, 12:25:10

so the values are in date format.

I appreciate any help

let endDateTime = moment
  .unix(parseInt(auctionEndDateTime))
  .format("MMMM Do YYYY, h:mm:ss");


let start=moment(Date.now());
let end=moment(endDateTime);
let duration = moment.duration(end.diff(start));
let hours = duration.asHours();


console.log('hours ' + hours);
console.log('days ' + duration.asDays());

Upvotes: 0

Views: 2219

Answers (1)

noone
noone

Reputation: 6558

// you can get the difference between two days with javascript Date class

let firstDate = new Date('Sun Nov 24 2019 17:28:33'); //new Date('2019-11-12');
let secondDate = new Date('Tue Nov 26 2019 17:28:33');//new Date('2019-11-20');

let milliSFirst = firstDate.getTime();
let milliSSecond = secondDate.getTime();

console.log("diff in days " + (milliSSecond - milliSFirst)/(1000 * 3600 * 24) )

// take the dates as milliseconds and then you can do the calculations

let start=moment(Date.now());
let end=moment(Date.now() + 1000 * 3600 * 24 );
let duration = moment.duration(end.diff(start));
let hours = duration.asHours();
 
 
console.log('hours ' + hours);
console.log('days ' + duration.asDays());
<script src="https://rawgit.com/moment/moment/2.2.1/min/moment.min.js"></script>

Upvotes: 1

Related Questions