【问题标题】:I have trouble with taking away the weekend in my code [duplicate]我无法在我的代码中取消周末 [重复]
【发布时间】:2019-09-02 12:04:04
【问题描述】:

我正在为学校制作一个网站页面,人们可以在其中输入两个日期:

  1. 开始为公司工作
  2. 一天停止为公司工作(合同结束时)

这些天以MM-DD-YYYY的格式填写

当一个人填写开始日期时,它会计算以下公式: “工作天数 = 今天的日期 - 该人开始工作的日期” 之后,它会将其计算为天而不是毫秒(工作天数/1000/60/60/24)。

现在我必须摆脱一个人每周工作的SaturdaySunday

编辑:已修复,谢谢大家

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);

【问题讨论】:

    标签: javascript date days weekend


    【解决方案1】:

    我相信this question 与您的相似。 该问题中接受的答案提供了此功能:

    // 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;
    }
    

    【讨论】:

    • 这对我很有帮助,谢谢!!
    【解决方案2】:

    我会这样做:

    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

    【讨论】:

      猜你喜欢
      • 2015-11-11
      • 2013-06-28
      • 2022-01-17
      • 2018-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-20
      • 2018-01-30
      相关资源
      最近更新 更多