我无法在我的代码中带走周末
I have trouble with taking away the weekend in my code
我正在为学校制作一个网站页面,人们可以在其中输入两个日期:
- 天开始为公司工作
- 天停止为公司工作(合同结束时)
这些天数的填写格式为MM-DD-YYYY
当一个人填写开始日期时,它将计算以下公式:
"days worked = todays date - the date that the person has started working"
之后它将计算为天而不是毫秒(天 worked/1000/60/60/24)。
现在我必须摆脱一个人每周工作的 Saturday
和 Sunday
。
编辑:已修复,谢谢大家
Javascript代码
function days_of_a_year(year) { return isLeapYear(year) ? 366 : 365; }
function isLeapYear(year) { return year % 400 === 0 || (year % 100 !== 0 && year % 4 === 0); }
var year = moment().year();
var days_year = days_of_a_year(moment().year());
$(document).ready(function(){
$(".form-control").keyup(function(){
//get
var leave_days = $('#leave_days').val(),
leave_hours = $('#leave_hours').val(),
hours_employee_week = $('#hours_employee_week').val(),
hours_week = $('#hours_week').val(),
date_employed = $('#date_employed').val(),
date_unemployed = $('#date_unemployed').val(),
start = new Date(date_employed),
end = new Date(date_unemployed),
diff = new Date(end - start),
days = Math.round(diff/1000/60/60/24),
now = new Date(),
days_worked = new Date(now - start),
year = moment().year();
var leave_hours_full = leave_days * leave_hours;
var perc_employment = hours_employee_week / hours_week * 100;
var leave_hours_year = leave_hours_full * (hours_employee_week / hours_week);
var days_worked_year = Math.round(days_worked/1000/60/60/24);
console.log(parseInt(days_worked_year));
$('#days_worked_year').val(days);
$('#days_full_year').val(days_year);
$('#perc_worked_year').val(days_worked_year);
我相信 与您的相似。
该问题中接受的答案提供了这个功能:
// Expects start date to be before end date
// start and end are Date objects
function dateDifference(start, end) {
// Copy date objects so don't modify originals
var s = new Date(+start);
var e = new Date(+end);
// Set time to midday to avoid dalight saving and browser quirks
s.setHours(12,0,0,0);
e.setHours(12,0,0,0);
// Get the difference in whole days
var totalDays = Math.round((e - s) / 8.64e7);
// Get the difference in whole weeks
var wholeWeeks = totalDays / 7 | 0;
// Estimate business days as number of whole weeks * 5
var days = wholeWeeks * 5;
// If not even number of weeks, calc remaining weekend days
if (totalDays % 7) {
s.setDate(s.getDate() + wholeWeeks * 7);
while (s < e) {
s.setDate(s.getDate() + 1);
// If day isn't a Sunday or Saturday, add to business days
if (s.getDay() != 0 && s.getDay() != 6) {
++days;
}
}
}
return days;
}
这是我的做法:
function daysWorked(firstDay, lastDay) {
let daysWorked = 0;
for (
let cursor = new Date(+firstDay);
cursor.getTime() <= lastDay.getTime();
cursor.setDate(cursor.getDate() + 1)
) {
let day = cursor.getDay();
// skip Saturdays and Sundays
if (day === 0 || day === 6) {
continue;
}
daysWorked++;
}
return daysWorked;
}
我觉得这可能比以前的答案效率低一些,具体取决于 JavaScript 处理日期的效果。
让我试一下基准测试...
从 2019-06-16 到 2019-09-02
- 100 万次
dateDifference()
来自上一个答案:3698 毫秒
- 100 万次
daysWorked()
来自我的回答:47979 毫秒
慢 12 倍...
从 2015-06-16 到 2019-09-02
- 10k 次
dateDifference()
来自上一个答案:95 毫秒
- 10k 次
daysWorked()
我的回答:9054 毫秒
~慢100倍...
好吧忘记我的回答 xD
我正在为学校制作一个网站页面,人们可以在其中输入两个日期:
- 天开始为公司工作
- 天停止为公司工作(合同结束时)
这些天数的填写格式为MM-DD-YYYY
当一个人填写开始日期时,它将计算以下公式: "days worked = todays date - the date that the person has started working" 之后它将计算为天而不是毫秒(天 worked/1000/60/60/24)。
现在我必须摆脱一个人每周工作的 Saturday
和 Sunday
。
编辑:已修复,谢谢大家
Javascript代码
function days_of_a_year(year) { return isLeapYear(year) ? 366 : 365; }
function isLeapYear(year) { return year % 400 === 0 || (year % 100 !== 0 && year % 4 === 0); }
var year = moment().year();
var days_year = days_of_a_year(moment().year());
$(document).ready(function(){
$(".form-control").keyup(function(){
//get
var leave_days = $('#leave_days').val(),
leave_hours = $('#leave_hours').val(),
hours_employee_week = $('#hours_employee_week').val(),
hours_week = $('#hours_week').val(),
date_employed = $('#date_employed').val(),
date_unemployed = $('#date_unemployed').val(),
start = new Date(date_employed),
end = new Date(date_unemployed),
diff = new Date(end - start),
days = Math.round(diff/1000/60/60/24),
now = new Date(),
days_worked = new Date(now - start),
year = moment().year();
var leave_hours_full = leave_days * leave_hours;
var perc_employment = hours_employee_week / hours_week * 100;
var leave_hours_year = leave_hours_full * (hours_employee_week / hours_week);
var days_worked_year = Math.round(days_worked/1000/60/60/24);
console.log(parseInt(days_worked_year));
$('#days_worked_year').val(days);
$('#days_full_year').val(days_year);
$('#perc_worked_year').val(days_worked_year);
我相信
// Expects start date to be before end date
// start and end are Date objects
function dateDifference(start, end) {
// Copy date objects so don't modify originals
var s = new Date(+start);
var e = new Date(+end);
// Set time to midday to avoid dalight saving and browser quirks
s.setHours(12,0,0,0);
e.setHours(12,0,0,0);
// Get the difference in whole days
var totalDays = Math.round((e - s) / 8.64e7);
// Get the difference in whole weeks
var wholeWeeks = totalDays / 7 | 0;
// Estimate business days as number of whole weeks * 5
var days = wholeWeeks * 5;
// If not even number of weeks, calc remaining weekend days
if (totalDays % 7) {
s.setDate(s.getDate() + wholeWeeks * 7);
while (s < e) {
s.setDate(s.getDate() + 1);
// If day isn't a Sunday or Saturday, add to business days
if (s.getDay() != 0 && s.getDay() != 6) {
++days;
}
}
}
return days;
}
这是我的做法:
function daysWorked(firstDay, lastDay) {
let daysWorked = 0;
for (
let cursor = new Date(+firstDay);
cursor.getTime() <= lastDay.getTime();
cursor.setDate(cursor.getDate() + 1)
) {
let day = cursor.getDay();
// skip Saturdays and Sundays
if (day === 0 || day === 6) {
continue;
}
daysWorked++;
}
return daysWorked;
}
我觉得这可能比以前的答案效率低一些,具体取决于 JavaScript 处理日期的效果。
让我试一下基准测试...
从 2019-06-16 到 2019-09-02
- 100 万次
dateDifference()
来自上一个答案:3698 毫秒 - 100 万次
daysWorked()
来自我的回答:47979 毫秒
慢 12 倍...
从 2015-06-16 到 2019-09-02
- 10k 次
dateDifference()
来自上一个答案:95 毫秒 - 10k 次
daysWorked()
我的回答:9054 毫秒
~慢100倍...
好吧忘记我的回答 xD