在javascript中查找给定两天之间存在的天数,月数和年数[重复]

时间:2022-08-25 18:17:32

This question already has an answer here:

这个问题在这里已有答案:

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.

例如,对于CalcEnddate =“2012年12月30日”和visualEnddate =“2013年3月31日”,它返回我的月数= 2,天= 2。

Please help.

3 个解决方案

#1


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

#2


0  

You could do it so easily using momentjs.

你可以使用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>

#3


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.

它返回2个月30天作为输出。

#1


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

#2


0  

You could do it so easily using momentjs.

你可以使用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>

#3


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.

它返回2个月30天作为输出。