Rene
0
Q:

momentjs number of days between two dates

function dateDiffInDays(date1, date2) {
    // round to the nearest whole number
    return Math.round((date2-date1)/(1000*60*60*24));
}
var daysDiff=dateDiffInDays(new Date('December 21, 2019 03:24:00'), new Date('December 25, 2019 03:24:00')); //4 days till XMAS
4
// moment version => 2.13.0.
const start = moment().subtract(1, 'days');
const end = new Date();
const actual = moment().subtract(1, 'hours');
const test = moment(actual).isBetween(start, end);
console.log(test);
  
// moment version < 2.13.0.
var startDate = new Date(2013, 1, 12)
  , endDate   = new Date(2013, 1, 15)
  , date  = new Date(2013, 2, 15)
  , range = moment().range(startDate, endDate);
range.contains(date);
1
var given = moment("2018-03-10", "YYYY-MM-DD");
var current = moment().startOf('day');

//Difference in number of days
moment.duration(given.diff(current)).asDays();
1
/* difference between date1 and date2 in days (date2 - date1) */
/* date1 and date 2 are already javascript date objects */
function dateDifference(date2, date1) {
    const _MS_PER_DAY = 1000 * 60 * 60 * 24;

    // Discard the time and time-zone information.
    const utc1 = Date.UTC(date1.getFullYear(), date1.getMonth(), date1.getDate());
    const utc2 = Date.UTC(date2.getFullYear(), date2.getMonth(), date2.getDate());

    return Math.floor((utc2 - utc1) / _MS_PER_DAY);
}
2
const diffDays = (date, otherDate) => Math.ceil(Math.abs(date - otherDate) / (1000 * 60 * 60 * 24));

// Example
diffDays(new Date('2014-12-19'), new Date('2020-01-01'));   // 1839
1

New to Communities?

Join the community