合同工期计算器 - 在线计算合同天数、周数、月数、工作日
var startDate = new Date(start); var endDate = new Date(end); var days = (endDate - startDate) / (1000 * 60 * 60 * 24); return days; };
// 根据起止日期计算合同工期日历月数 exports.getContractMonthByDate = function(start, end) { // 根据起止日期计算合同工期日历月数 var startDate = new Date(start); var endDate = new Date(end); var month = 0; month = (endDate.getFullYear() - startDate.getFullYear()) * 12; month -= startDate.getMonth() + 1; month += endDate.getMonth() + 1; return month; };
// 根据起止日期计算合同工期日历周数 exports.getContractWeekByDate = function(start, end) { // 根据起止日期计算合同工期日历周数 var startDate = new Date(start); var endDate = new Date(end); var weeks = 0; var day = (endDate - startDate) / (1000 * 60 * 60 * 24); if (day % 7 == 0) { weeks = day / 7; } else { weeks = Math.floor(day / 7) + 1; } return weeks; };
// 根据起止日期计算合同工期日历工作日数 exports. getContractWorkdayByDate = function(start, end) { // 根据起止日期计算合同工期日历工作日数 var startDate = new Date(start); var endDate = new Date(end); var workday = 0; var days = 0; //获取天数 days = (endDate - startDate) / (1000 * 60 * 60 * 24); //计算工作日 for (var i = 0; i <= days; i++) { var tempDate = new Date(startDate); tempDate.setDate(startDate.getDate() + i); if (tempDate.getDay() !== 0 && tempDate.getDay() !== 6) { workday++; } } return workday; };
// 根据起止日期计算合同工期日历工作日 exports.getContractWorkdaysByDate = function(start, end) { // 根据起止日期计算合同工期日历工作日 var startDate = new Date(start); var endDate = new Date(end); var workdays = 0; var days = 0; var result = []; //获取天数 days = (endDate - startDate) / (1000 * 60 * 60 * 24); //计算工作日 for (var i = 0; i <= days; i++) { var tempDate = new Date(startDate); tempDate.setDate(startDate.getDate() + i); if (tempDate.getDay() !== 0 && tempDate.getDay() !== 6) { workdays++; result.push(new Date(tempDate).format('yyyy-MM-dd')); } } return result; };
// 根据起止日期计算合同工期日历周末数 exports.getContractWeekendByDate = function(start, end) { // 根据起止日期计算合同工期日历周末数 var startDate = new Date(start); var endDate = new Date(end); var weekend = 0; var days = 0; //获取天数 days = (endDate - startDate) / (1000 * 60 * 60 * 24); //计算周末 for (var i = 0; i <= days; i++) { var tempDate = new Date(startDate); tempDate.setDate(startDate.getDate() + i); if (tempDate.getDay() === 0 || tempDate.getDay() === 6) { weekend++; } } return weekend; };
// 根据起止日期计算合同工期日历周末 exports.getContractWeekendsByDate = function(start, end) { // 根据起止日期计算合同工期日历周末 var startDate = new Date(start); var endDate = new Date(end); var weekends = 0; var days = 0; var result = []; //获取天数 days = (endDate - startDate) / (1000 * 60 * 60 * 24); //计算周末 for (var i = 0; i <= days; i++) { var tempDate = new Date(startDate); tempDate.setDate(startDate.getDate() + i); if (tempDate.getDay() === 0 || tempDate.getDay() === 6) { weekends++; result.push(new Date(tempDate).format('yyyy-MM-dd')); } } return result; };
// 根据起止日期计算合同工期日历 exports.getContractDateByDate = function(start, end) { // 根据起止日期计算合同工期日历 var startDate = new Date(start); var endDate = new Date(end); var days = 0; var result = []; //获取天数 days = (endDate - startDate) / (1000 * 60 * 60 * 24); //计算周末 for (var i = 0; i <= days; i++) { var tempDate = new Date(startDate); tempDate.setDate(startDate.getDate() + i); result.push(new Date(tempDate).format('yyyy-MM-dd')); } return result; };
// 根据起止日期计算合同工期 exports.getContractCalendarByDate = function(start, end) { // 根据起止日期计算合同工期 var startDate = new Date(start); var endDate = new Date(end); var result = { day: 0, week: 0, month: 0, workday: 0, workdays: [], weekend: 0, weekends: [], calendar: [] }; var days = 0; //获取天数 result.day = (endDate - startDate) / (1000 * 60 * 60 * 24); //计算工作日 for (var i = 0; i <= days; i++) { var tempDate = new Date(startDate); tempDate.setDate(startDate.getDate() + i); if (tempDate.getDay() !== 0 && tempDate.getDay() !== 6) { result.workday++; result.workdays.push(new Date(tempDate).format('yyyy-MM-dd')); } } //计算周末 for (var i = 0; i <= days; i++) { var tempDate = new Date(startDate); tempDate.setDate(startDate.getDate() + i); if (tempDate.getDay() === 0 || tempDate.getDay() === 6) { result.weekend++; result.weekends.push(new Date(tempDate).format('yyyy-MM-dd')); } } //计算日历 for (var i = 0; i <= days; i++) { var tempDate = new Date(startDate); tempDate.setDate(startDate.getDate() + i); result.calendar.push(new Date(tempDate).format('yyyy-MM-dd')); } result.week = Math.floor(result.day / 7) + 1; result.month = (endDate.getFullYear() - startDate.getFullYear()) * 12 + endDate.getMonth() - startDate.getMonth() + 1; return result; };
原文地址: https://www.cveoy.top/t/topic/liRv 著作权归作者所有。请勿转载和采集!