2

Been trying to resolve this one for a while with no success, I found couple of similar answers here, but the format is what is important here. I need to return X years, X months, X days.

Can you take a look to see, what I am doing wrong here... Number of days are not quite right.

Here is a bin

function inBetweenDays(y,m,d){

var user_date = new Date(y,m + 1,d);
var today_date = new Date();

  
var diff_date = (user_date - today_date);

var num_years = diff_date/31536000000;
var num_months = (diff_date % 31536000000)/2628000000;
var num_days =  ((diff_date % 31536000000) % 2628000000)/86400000;
 
var years = Math.floor(Math.abs(num_years));
var months = Math.floor(Math.abs(num_months));  
var days =  Math.floor(Math.abs(num_days));
  
  if (years >= 1) {
    console.log(years + " years " + months + " months " + days + " days");
  } else if (years <= 0 && months >= 0){
     console.log(months + " months " + days + " days");
   } else {
     console.log(days + " days ");
   }
  
  }
  

inBetweenDays(2015,03,04);
inBetweenDays(2016,03,04);
inBetweenDays(2016,02,04);
inBetweenDays(2018,02,04);
Lucky500
  • 677
  • 5
  • 17
  • 31
  • 1
    Shouldn't the third line be `new Date(y,m - 1,d)` instead? – Nayuki Feb 05 '16 at 01:46
  • You are going wrong already in assuming a day was `86400000` milliseconds long. Please go and research why. – CBroe Feb 05 '16 at 01:48
  • @Nayuki... I think it should be m + 1, since month is 0 based..... – Lucky500 Feb 05 '16 at 01:57
  • Are you looking for the _calendar_ difference between two dates? If you want March 1 to be 2 months, 0 days after Jan 1 irrespective of leap year, it will be hard to use milliseconds between two dates to arrive at the answer. – traktor Feb 05 '16 at 02:03
  • @Lucky500 Just because is 0 based, you should use real month -1,and can get the true param month.But sadly it's not the only mistake.I think may be this concept is wrong, because `one day` is base on 24 hours is a certain value, but `month` and `year` a not certain collection about day, some month have different days and so does year. You have to consider it first. – Jelly Feb 05 '16 at 02:08
  • Thanks guys, I will work on tweaking it a bit more. – Lucky500 Feb 05 '16 at 02:13
  • 1
    Your result is almost certainly wrong for periods longer than a few months, since the length of days, months and years varies. Are you sure this isn't a duplicate of [*Return Date in format ( x years, x months, x days)*](http://stackoverflow.com/questions/35189490/return-date-in-format-x-years-x-months-x-days-javascript) (which is itself a duplicate)? You probably should zero the hours for *today_date*. – RobG Feb 05 '16 at 02:43
  • @CBroe a day **IS** 86400000 ms long. Please go and research why. – Charlie74 Feb 05 '16 at 04:30
  • @Charlie74—except when daylight saving starts or ends. – RobG Feb 05 '16 at 04:34

3 Answers3

1

An algorithm to calculate the calendar date difference between two date in terms of years, months, days is as follows:

  1. validate dates to have full years (4 digits), months in range 1-12, and month-day in range 1 - days in month of year of date.

  2. subtract earlier date days from later date days. If borrow required, add days in the month of earlier date, in the year of the earlier date, to later date days and add 1 to months to be subtracted.

  3. subtract earlier date month from later date months. If borrow required, add 12 to later day months and add 1 to years to be subtracted.

  4. subtract earlier date year from later date year.

This may have been implemented in one existing answer to this question in a chain of duplicate questions, but try and spot it without documentation of the algorithm. The calculation of days to borrow can be calculated as follows:

function monthDays(y, m)    // full year and month in range 1-12
{   var leap = 0;
    if( m == 2)
    {   if( y % 4 == 0) leap = 1;
        if( y % 100 == 0) leap = 0;
        if( y % 400 == 0) leap = 1;
    }
    return [0, 31,28,31,30,31,30,31,31,30,31,30,31][ m] + leap;
}

(Edit) or alternatively as per comment, using the behavior of Date objects adjusting to out of expected bound behavior:

function monthDays( y, m)
{ return new Date(y, m, 0).getDate();
}
traktor
  • 17,588
  • 4
  • 32
  • 53
  • 1
    If *m* is calendar month number, then `new Date(y, m, 0).getDate()` returns the number of days in the month and is less to type. ;-) – RobG Feb 05 '16 at 04:37
  • @RobG I like it. Would I like to explain it to a novice? ;-) – traktor Feb 05 '16 at 05:01
  • 1
    Using the calendar month number sets the date to the next month (since ECMAScript months are 0 based), but setting the date to zero creates a date for the last day of the previous month, so back to the month you want, e.g. `new Date(2016,1,0)` creates a date for 2016-01-31. – RobG Feb 05 '16 at 09:34
0

HTML

<input id="first" value="1/1/2000"/>
<input id="second" value="1/1/2001"/>

<script>
  alert(datediff("day", first, second)); // what goes here?
</script>

JavaScript

function parseDate(str) {
    var mdy = str.split('/')
    return new Date(mdy[2], mdy[0]-1, mdy[1]);
}

function daydiff(first, second) {
    return Math.round((second-first)/(1000*60*60*24));
}

Source: How do I get the number of days between two dates in JavaScript?

Community
  • 1
  • 1
Sami
  • 3,686
  • 4
  • 17
  • 28
0
function days_between(date1, date2) {

    // The number of milliseconds in one day
    var ONE_DAY = 1000 * 60 * 60 * 24;

    // Convert both dates to milliseconds
    var date1_ms = date1.getTime();
    var date2_ms = date2.getTime();

    // Calculate the difference in milliseconds
    var difference_ms = Math.abs(date1_ms - date2_ms);

    // Convert back to days and return
    return Math.round(difference_ms / ONE_DAY);
}
Mahesh
  • 1,063
  • 1
  • 12
  • 29