我想查找多少天,幾個月和幾年存在於兩個日期之間。 這是我目前的方法,我使用查找在給定的兩天之間存在的天數,月數和年數在javascript中
$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 ;
}
但它給了我錯誤的結果。
例如,對於CalcEnddate = "December 30, 2012"
和visualEnddate = "March 31, 2013"
它返回我的編號months = 2
,days = 2
。
請幫忙。
請通過這個,你會得到更好的主意:http://stackoverflow.com/questions/4881938/javascript-calculate-number-of-days-in-month-for-a-given-year – Jigar7521
所以天在你的情況下應該是92? – Mahi
查看[this](http://stackoverflow.com/a/40038836/4331291)回答 – Nishant123