0

I want find hows many days, Months and years exists in between two date. This is my current method that i am using

 $scope.getForcastPeriod = function(newValue){

        var CalcEnddate;
        var forecastDays;



            var CalcEnddate   = new Date($scope.forecastObj.paramObj.enddate);
            var visualEnddate = new Date($scope.visualDate.enddate);

            var diff = new Date(visualEnddate - CalcEnddate);

            var years = (diff.getFullYear() - 1970);
            var months = (diff.getMonth()) + (12 * years);
            var days = (diff.getDate()) + (365 * years);

            var months = $scope.monthDiff(CalcEnddate,visualEnddate);


            if($scope.forecastObj.paramObj.interval == "Yearly"){
                forecastDays = years+1;
            }
            else if($scope.forecastObj.paramObj.interval == "Monthly"){
                forecastDays = months+1;
            }
            else if($scope.forecastObj.paramObj.interval == "Daily"){
                forecastDays = days;
            }

            return forecastDays ;

    }

But it gives me wrong results.

For example, for CalcEnddate = "December 30, 2012" and visualEnddate = "March 31, 2013" it returns me number of months = 2, days = 2.

Please help.

denny
  • 197
  • 1
  • 1
  • 11

3 Answers3

1

You can simply do this,

var date1 = new Date("12/30/2012");
var date2 = new Date("3/31/2013");
var timeDiff = Math.abs(date2.getTime() - date1.getTime());
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24)); 
var diffmonths = Math.ceil(timeDiff / (1000 * 3600 * 24 *12)); 
alert("DAYS"+ " " +diffDays);
alert("MONTHS"+ " " + diffmonths);

JSFIDDLE

Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
0

You could do it so easily using momentjs.

Here is the solution to find the difference.

var dateOne = moment();
var dateTwo = moment("2016-10-10T10:00:00");
console.log(moment.duration(dateTwo.diff(dateOne)).humanize());

var dateThree = moment("2014-10-10T10:00:00");
console.log(moment.duration(dateThree.diff(dateOne)).humanize());

var dateFour = moment("2016-10-16T10:00:00");
console.log(moment.duration(dateFour.diff(dateOne)).humanize());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.15.1/moment.min.js"></script>
Sreekanth
  • 3,110
  • 10
  • 22
  • This doesn't answer the question and it relies on a library that is not tagged or asked for. – RobG Oct 18 '16 at 00:43
0

javascript logic

<html>
<head>
<title>Date substraction</title>
</head>
<body>
<script language="JavaScript" type="text/javascript">
<!--

var startDate = prompt("Enter a start date : (mm/dd/yyyy format) ")
var endDate = prompt("Enter a end date: (mm/dd/yyyy format)")

var start_date = Date.parse(startDate);
var end_date = Date.parse(endDate);

var diff_date =  end_date - start_date;

var num_years = diff_date/31536000000;
var num_months = (diff_date % 31536000000)/2628000000;
var num_days = ((diff_date % 31536000000) % 2628000000)/86400000;

document.write("Number of years: " + Math.floor(num_years) + "<br>");
document.write("Number of months: " + Math.floor(num_months) + "<br>");
document.write("Number of days: " + Math.floor(num_days) + "<br>");
//-->
</script>
</body>
</html>

Ref: http://www.java2s.com/Tutorial/JavaScript/0240__Date/Getyearmonthanddayfromdatedifference.htm

It returns 2 months 30 days as output.

Senthil
  • 2,156
  • 1
  • 14
  • 19