javascript 如何计算两个选定日历日期之间的总天数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29933608/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to calculate the total days between two selected calendar dates
提问by Bryan
Let say i have startDate = 7/16/2015 and endDate = 7/20/2015. This 2 dates are stored in a SharePoint list.
假设我有 startDate = 7/16/2015 和 endDate = 7/20/2015。这 2 个日期存储在 SharePoint 列表中。
If user select the exact date with the date in SharePoint list, it can calculate the total days = 2 , which means that without calculate on the other days.
如果用户选择与 SharePoint 列表中的日期相同的日期,则可以计算出总天数 = 2 ,这意味着不计算其他天数。
Anyone can please help on this?
任何人都可以请帮忙吗?
I use the following code to calculate the total day of difference without counting on weekend. But I cant figure out the way how to calculate the total day of selected date without counting on other days.
我使用以下代码计算差异的总天数,而不计算周末。但我无法弄清楚如何在不计算其他日期的情况下计算所选日期的总天数。
function workingDaysBetweenDates(startDate,endDate) {
// Validate input
if (endDate < startDate)
return 'Invalid !';
// Calculate days between dates
var millisecondsPerDay = 86400 * 1000; // Day in milliseconds
startDate.setHours(0,0,0,1); // Start just after midnight
endDate.setHours(23,59,59,999); // End just before midnight
var diff = endDate - startDate; // Milliseconds between datetime objects
var days = Math.ceil(diff / millisecondsPerDay);
// Subtract two weekend days for every week in between
var weeks = Math.floor(days / 7);
var days = days - (weeks * 2);
// Handle special cases
var startDay = startDate.getDay();
var endDay = endDate.getDay();
// Remove weekend not previously removed.
if (startDay - endDay > 1)
days = days - 2;
// Remove start day if span starts on Sunday but ends before Saturday
if (startDay == 0 && endDay != 6)
days = days - 1;
// Remove end day if span ends on Saturday but starts after Sunday
if (endDay == 6 && startDay != 0)
days = days - 1;
return days;
}
回答by Vadim Gremyachev
The following function calculates the number of business days between two dates
以下函数计算两个日期之间的工作日数
function getBusinessDatesCount(startDate, endDate) {
var count = 0;
var curDate = startDate;
while (curDate <= endDate) {
var dayOfWeek = curDate.getDay();
if(!((dayOfWeek == 6) || (dayOfWeek == 0)))
count++;
curDate.setDate(curDate.getDate() + 1);
}
return count;
}
//Usage
var startDate = new Date('7/16/2015');
var endDate = new Date('7/20/2015');
var numOfDates = getBusinessDatesCount(startDate,endDate);
$('div#result').text(numOfDates);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="result"/>
回答by Kevin Kuyl
First you have to calculate the difference in time, then convert the time to days
首先您必须计算时间差,然后将时间转换为天
var calculateDifference = function(date1, date2){
var timeDifference = Math.abs(date2.getTime() - date1.getTime());
return Math.ceil(timeDifference / (1000 * 3600 * 24));//ms * seconds * hours
}
var difference = calculateDifference(new Date("7/16/2015"), new Date("7/20/2015"));
untested, but should work...
未经测试,但应该工作......
回答by Prophet Daniel
Using momentstartDate
and endDate
:
使用时刻startDate
和endDate
:
function getBusinessDaysCount(startDate, endDate) {
let relativeDaySequence = [...Array(endDate.diff(startDate, "days")).keys()];
let daySequence = relativeDaySequence.map(relativeDay => {
let startDateClone = startDate.clone();
return startDateClone.add(relativeDay, "days");
});
return daySequence.reduce(
(dayCount, currentDay) => dayCount + (currentDay.day() === 0 || currentDay.day() === 6 ? 0 : 1),
0
);
}
回答by Oubaid Ahmed
Can Try this it will also work.
可以试试这个它也行。
//fromDate is start date and toDate is end Date
var timeDiff = Math.abs(toDate.getTime() - fromDate.getTime());
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24)) ;
// var startDay = fromDate.getDay();
debugger;
var diffDayWeek = diffDays + fromDate.getDay();
var diffDiv = Math.floor(parseInt(diffDayWeek/7));
diffDiv *= 2; "Saturday and Sunday are Weekend
diffDays += 1 - diffDiv;