how to find difference in days between day and month irrespective of years

I want to know the difference between to two dates irrespective of year..

For Example : format date/month/year

For example difference of today date to some date lets take 01/06

The expected answer for this will be around 185 days..

I tried below example..Let me know whats wrong with this

   var a = moment('06/01','M/D');
   console.log(a);
   var b = moment();
   console.log(b);

   var diffDays = b.diff(a, 'days');
   alert(diffDays);

I dont want to use momet.js atmost. If it can be done with javascript its so good for me.

Upvotes: 1

Views: 139

Answers (4)

Xotic750
Xotic750

Reputation: 23482

If it is this year then I am getting a difference of 147 using a library that I have been working on (AstroDate) which doesn't rely on javascript's Date object, it's all done with pure math.

require.config({
    paths: {
        'astrodate': '//rawgit.com/Xotic750/astrodate/master/lib/astrodate'
    }
});

require(['astrodate'], function (AstroDate) {
    "use strict";
    
    var diff = new AstroDate("2015","6","1").jd() - new AstroDate("2015","1","5").jd();
  
    document.body.appendChild(document.createTextNode(diff));
});
<script src="http://requirejs.org/docs/release/2.1.8/minified/require.js"></script>

If it was next year, which is a leap year then I am getting 148

require.config({
    paths: {
        'astrodate': '//rawgit.com/Xotic750/astrodate/master/lib/astrodate'
    }
});

require(['astrodate'], function (AstroDate) {
    "use strict";

    var diff = new AstroDate("2016", "6", "1").jd() - new AstroDate("2016", "1", "5").jd();

    document.body.appendChild(document.createTextNode(diff));
});
<script src="http://requirejs.org/docs/release/2.1.8/minified/require.js"></script>

Upvotes: 0

charlietfl
charlietfl

Reputation: 171669

Can try using this:

var str1 = '06/01', str2 = '02/28', d1, d2, diff;

function setDate(str, date) {
    var date = new Date(),
        dateParts = str.split('/'),
        monthIndex = parseInt(dateParts[0], 10) - 1,
        day = parseInt(dateParts[1], 10);

    date.setMonth(monthIndex);
    date.setDate(day);        
    return date
}

d1 = setDate(str1);
d2 = setDate(str2);
diff = Math.round(Math.abs((d1 - d2) / (24 * 60 * 60 * 1000)))
console.log(diff) // returns 93

The rounding is due to differences in daylight savings (or other locale time shifts within the year) that can cause decimal values returned.

It is probably better to use UTC for this

If current year is leap year and dates span end of February then Feb 29 would also be counted

DEMO

Upvotes: 0

xbakesx
xbakesx

Reputation: 13500

Barth Zaleweski is 100% on track with that. If you want to use straight javascript:

    var today = new Date();
    var otherDate = new Date(today);

    otherDate.setMonth(5); // Set the month (on scale from 0 to 11)
    otherDate.setDate(1); // set day

    var seconds = (otherDate.getTime() - today.getTime()) / 1000;
    var minutes = seconds / 60;
    var hours = minutes / 60;
    var days = hours / 24;
    console.log(days);

There are methods for setting hour/minute/second as well, but if you don't do anything they'll be the same as the start, and you can obviously call those same methods on your start time if you don't want to use today.

Upvotes: 1

Liglo App
Liglo App

Reputation: 3819

A nice trick could be to set the year to always the same.

   var a = moment('2015/06/01','Y/M/D');
   console.log(a);
   var b = moment().set('year', 2015);
   console.log(b);

   var diffDays = b.diff(a, 'days');
   alert(diffDays);

The problem about your question in general is how to deal with leap years; how the script should know the difference between 2/20 and 3/1 ? You have to consider how to solve this.

Upvotes: 3

Related Questions