javascript - 从日期/时间小时计数中排除周末

标签 javascript internet-explorer-8

我有以下内容可以很好地满足我的需求:

function funLoad(str1,str3,str4)
{

    var dym1 = str1.split("/");

    var d=new Date();
    var dym2 = d.getMonth() + 1 + "-" + d.getDate() + "-" + d.getFullYear() + " " + d.getHours() + ":" + d.getMinutes() + ":00";
    //var dym2 = "6 10 2013 09:00:00";

    var start = Date.parse(dym1[1] + "-" + dym1[0] + "-" + dym1[2] + " " + str3 + ":" + str4 + ":00"); 
    var end = Date.parse(dym2);

    return (start-end) / (1000*60*60);

}

$("#btn1").click(function(event){
    alert(funLoad($("#txt1").val(),$("#ddlHourTime").val(),$("#ddlMinuteTime").val()));
});

这是一个 jsfiddle: http://jsfiddle.net/QTVWd/8/

我知道这可能只适用于 IE8,但我是在每个人都只使用 IE8 的 Intranet 环境中开发的,所以这不是问题。忽略这一点,此脚本仅获取选定的日期和时间,将其与当前日期/时间进行比较,然后返回小时数。到目前为止一切都很好。

问题在于它计算了周末。如果开始和结束日期包括周末,我如何只计算工作日的小时数并排除周末?例如,如果开始日期是星期五,结束日期是星期一,则应该只计算星期五和星期一的小时数,不包括周末的 48 小时。

最佳答案

我在 jsfiddle 中编写的这个函数应该可以满足您的需求。您需要按照它的步调运行它,以确保它正确计算所有内容。

updated jsfiddle

函数 funLoad(str1, str3, str4) {

var dym1 = str1.split("/");

// get the given(start) epoch value (using midnight for time - will adjust for actual time further below)
var start_date = new Date(dym1[2], (dym1[1]-1), dym1[0], 0, 0, 0, 0);    
var start_epoch = start_date.getTime();

// get todays(end) epoch value (using midnight for time - will adjust for actual time further below)    
var end_d = new Date();
var end_epoch = new Date(end_d.getFullYear(), end_d.getMonth(), end_d.getDate(), 0, 0, 0, 0).getTime();
var end_date_is_weekend = (end_d.getDay()==0 || end_d.getDay()==6)?1:0;    

// get value for 1 day in milliseconds
var one_day = 1000*60*60*24; 

// how many days between given date and todays date
var total_days = Math.ceil( (end_epoch - start_epoch ) / one_day)-1;

// figure out how many of those total days are weekend days
var date_to_check = start_epoch + one_day; 
var total_weekend_days = 0;
var next_day_is_weekend = 0;
for(var x=0; x<total_days; x++){
    d=new Date(date_to_check);
    if(d.getDay()==0 || d.getDay()==6){
        total_weekend_days++;
        if(x==0){next_day_is_weekend=1;}
    }
    date_to_check += one_day
}

// determine number of week days
var total_week_days = total_days - total_weekend_days;

// get the total hours from the total week days
var total_hours = total_week_days*24; 

// still need to add the remaining hours from the given(start) date and the number of hours that have already past in the end(todays) date

// add in the remaining hours left in the start date
var hours_remaining_in_start_date = 0;

// rounding up to next nearest start date hour
if(str4 > 30){
    if(str3 == 23 && next_day_is_weekend==0){
        // rounding up puts the start date into the start of the next day so subtract
        // one day from the total, UNLESS the next day is a weekend.
        total_week_days -= 1;

        hours_remaining_in_start_date = -24;

    }else{
        hours_remaining_in_start_date = 24 - (str3+1);                       
    }
}else{
    hours_remaining_in_start_date = 24 - str3;        
}    

total_hours += hours_remaining_in_start_date; 

    // add in the hours from the end date (today date), UNLESS today is a weekend
var hours_past_in_end_date = 0;
var end_hour = end_d.getHours();
if(end_date_is_weekend==0){
    // rounding up to next nearest end date hour
    if(end_d.getMinutes() > 30){
        if(end_hour == 23){
            // rounding up makes it a full day
            hours_past_in_end_date = 24;
        }else{
            // rounding up just adds an hour
            hours_past_in_end_date = end_hour+1;
        }
    }else{
        hours_past_in_end_date = end_hour;    
    }
}
total_hours += hours_past_in_end_date;



$('#result').html('start epoch:'+start_epoch+', end epoch:'+end_epoch+', total days'+total_days+', total weekends'+total_weekend_days+', total week days:'+total_week_days+', hours_remaining_in_start_date:'+hours_remaining_in_start_date+', hours_past_in_end_date:'+hours_past_in_end_date+', total hours:'+total_hours);

关于javascript - 从日期/时间小时计数中排除周末,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17085019/

相关文章:

javascript - Jquery,通过文本框值从下拉列表中设置选项

css - 在 IE8 中将文本旋转 270 度

flash - video.js - 控件未在 IE8 中显示

css - IE7、IE8支持css3媒体查询

javascript - Node v0.10.25 中的字符串原型(prototype)没有 'endsWith'

javascript - 在 Javascript 中从二维数组创建总计数组

javascript - React/Next.js ,打开对话框后页面滚动卡住

javascript - 如何使用 ES6 过滤 JSON 数据

html - 圆 Angular 在IE8中显示白边

javascript - 防止字母的按键功能 - 如何在输入按键之前加载功能