0

How do i get the exact number of days from a given date range? ex. given the following:

today = moment("2016-06-29")
due = moment("2016-05-13")

days_left = today.diff(due, 'days'); // returns 47

but i want it to display 1 month and 16 days ?

Casey SV
  • 130
  • 8

5 Answers5

5

You can use the Moment.js Precise Range plugin to display date/time ranges precisely, in a human-readable format.

var today = moment("2016-06-29");
var due = moment("2016-05-13");

var days_left = moment.preciseDiff(today, due); // '1 month 16 days'
icaru12
  • 1,522
  • 16
  • 21
  • thanks for this, we need to add true `var days_left = moment.preciseDiff(today, due, true);` to get the number and not the string value – Casey SV Jun 29 '16 at 05:08
1

A plain javascript method:

function calendarDiff( date1, date2){
    var later = false;

    if( date1 > date2){
       later = true;
       var temp = date2;
       date2 = date1;
       date1 = temp;
    }
    date1 = date1.split("-");
    var y1 = date1[0], m1 = date1[1], d1 = date1[2];
    date2 = date2.split("-");
    var y2 = date2[0], m2 = date2[1], d2 = date2[2];

    if( (d2 -= d1) < 0){
        d2 += new Date(y1, m1, 0).getDate(); // *note
        ++m1;
    }
    if( (m2 -= m1) < 0){
        m2 += 12;
        ++y1;
    }
    y2 = y2 - y1;
    return {years: y2, months: m2, days: d2, later: later};
}

The method takes two date strings of format "YYYY-MM-DD". The returned object contains the calendar date differences as numeric property values, with later set true if date1 is later than date2.

*Note: adds days in the month of the earlier date when needing to borrow, as per (my) answer and (RobG) comment to an earlier wrong date difference question.

Example

var today = "2016-06-29";
var due = "2016-05-13";
JSON.stringify( calendarDiff( today, due) );
//= {"years":0,"months":1,"days":16,"later":true}
Community
  • 1
  • 1
traktor
  • 17,588
  • 4
  • 32
  • 53
0

thanks for all your inputs, i was able to get the days using below not optimal but it works. i was wondering if there was a one line answer to it though

today = moment("2016-06-29")
due = moment("2016-05-13")
months_past_due = today.diff(due_date, 'months')
month_before_today = due_date.add(months_past_due, 'months')
days_past_due = today.diff(month_before_today, 'days')

alert(months_past_due + " "+ days_past_due)

one question, if:

today = moment("2015-02-28");
due_date = moment("2015-01-30");

should i consider it as 1 month or 29 days past due? the code above says its 29 days.

Casey SV
  • 130
  • 8
0

This could obviously be better but it works and I am happy with it now based on @traktor53 code (thank you!)

hope someone finds this useful

function formatDate(date) {
    var later = false;
    const date1 = new Date();
    const date2 = new Date(date);
    let resultString = '';
    if (date1 < date2) {
        return 'Recently';
    }
    var y1 = date1.getFullYear(),
        m1 = date1.getMonth() + 1,
        d1 = date1.getDate();

    var y2 = date2.getFullYear(),
        m2 = date2.getMonth() + 1,
        d2 = date2.getDate();

    if (y1 - y2) {
        if (y1 - y2 == 1) {
            return `a year ago`;
        }
        return `${y1 - y2} years ago`;
    }

    if (m1 - m2) {
        if (m1 - m2 == 1) {
            return `a month ago`;
        }
        return `${m1 - m2} months ago`;
    }

    if (d1 - d2) {
        if (d1 - d2 > 7) {
            return `${Math.floor(d1 - d2 / 7)} weeks ago`;
        }
        if (d1 - d2 == 7) {
            return `a week ago`;
        }
        if (d1 - d2 < 7) {
            return `${d1 - d2} days ago`;
        }
    }
}
Avi E. Koenig
  • 360
  • 5
  • 13
-1

Here's a pure javascript solution

d1 = new Date("2016-06-29"); 
d2 = new Date("2016-05-13"); 
differ = new Date(
    d1.getFullYear() - d2.getFullYear(),
    d1.getMonth() - d2.getMonth(),
    d1.getDate() - d2.getDate()
);

You can then get the difference year, month and days from the diff date object like differ.getYear(), differ.getMonth() and, differ.getDate()

Hector Barbossa
  • 5,506
  • 13
  • 48
  • 70
  • You may wish to consider the effect of leap years. By my account, the difference between Feb 1 and Mar 1 in a leap year is 29 days, in a non leap year 28, and is certainly never 31 days. – traktor Jun 29 '16 at 04:08