2016-10-17 42 views
0

我想查找多少天,幾個月和幾年存在於兩個日期之間。 這是我目前的方法,我使用查找在給定的兩天之間存在的天數,月數和年數在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

請幫忙。

+0

請通過這個,你會得到更好的主意:http://stackoverflow.com/questions/4881938/javascript-calculate-number-of-days-in-month-for-a-given-year – Jigar7521

+0

所以天在你的情況下應該是92? – Mahi

+0

查看[this](http://stackoverflow.com/a/40038836/4331291)回答 – Nishant123

回答

1

你可以簡單的做到這一點,

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

+1

這不包括年和月 –

+0

@Sajeetharan - 謝謝.. – denny

+0

@denny大聲笑,不客氣 – Sajeetharan

0

你可以很容易地使用momentjs來做到這一點。

以下是找到差異的解決方案。

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>

+0

這並不回答這個問題,它依賴於沒有標記或要求的庫。 – RobG

0

JavaScript邏輯

<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

它返回2個月30天作爲輸出。

+0

你知道,不是每個月都有30天abd不是每年有365天 –

相關問題