在JavaScript日期计算中排除周末 [英] exclude weekends in javascript date calculation

查看:139
本文介绍了在JavaScript日期计算中排除周末的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两套代码可以工作。需要帮助将它们组合成一个。



这段代码让我有两个日期之间的区别。功能完美:

  function test(){
var date1 = new Date(txtbox_1.value);
var date2 = new Date(txtbox_2.value);

var diff =(date2 - date1)/ 1000;
var diff = Math.abs(Math.floor(diff));

var days = Math.floor(diff /(24 * 60 * 60));
var leftSec = diff - days * 24 * 60 * 60;

var hrs = Math.floor(leftSec /(60 * 60));
var leftSec = leftSec - hrs * 60 * 60;

var min = Math.floor(leftSec /(60));
var leftSec = leftSec - min * 60;

txtbox_3.value =天+。 +小时}

源代码



@cyberfly下面的代码似乎有排除sat和sun这是我需要的答案。 来源。但是,它在jquery和上面的代码是在JS。因此,需要帮助组合,因为我缺乏这些知识:(

 < script type =text / javascript> 

$(#startdate,#enddate)。change(function(){

var d1 = $(#startdate)。val();
var d2 = $(#enddate)val();

var minutes = 1000 * 60;
var hours = minutes * 60;
var day = 24;

var startdate1 = getDateFromFormat(d1,dmy);
var enddate1 = getDateFromFormat(d2,dmy);

var days = calcBusinessDays (new Date(startdate1),new Date(enddate1));

if(days> 0)
{$(#noofdays)val(days);}
else
{$(#noofdays)。val(0);}


});

< / script>






编辑
尝试组合代码,这是我的示例,获取对象预期错误

  function test(){
var date1 = new Date(startdate.value);
var date2 = new Date(enddate.value);
var diff =(date2 - date1)/ 1000;
var diff = Math.abs(Math.floor(diff));
var days = Math.floor(diff /(24 * 60 * 60));
var leftSec = diff - days * 24 * 60 * 60;
var hrs = Math.floor(leftSec /(60 * 60));
var leftSec = leftSec - hrs * 60 * 60;
var min = Math.floor(leftSec /(60));
var leftSec = leftSec - min * 60;

var startdate1 = getDateFromFormat(startdate,dd / mm / yyyy hh:mm);
var enddate1 = getDateFromFormat(enddate,dd / mm / yyyy hh:mm);
days = calcBusinessDays(new Date(startdate1),new Date(enddate1));
noofdays.value = days +。 +小时}

start:< input type =textid =startdatename =startdatevalue =02/03/2015 00:00>
end:< input type =textid =enddatename =enddatevalue =02/03/2015 00:01>
< input type =textid =noofdaysname =noofdaysvalue =>

解决方案

当确定两个日期之间的天数时,有很多关于什么是一天的决定。例如,2月1日至2月2日期间一般为一天,所以2月1日至2月1日为零日。



当增加计数工作日的复杂性时,事情变得更加艰难例如。 2015年2月2日星期一至5月6日星期五为4日(星期一至星期二为1,星期一至星期三为2等),但星期一至星期五一般被视为5个工作日,持续时间为2月2日到2月7日星期六也应该是4个工作日,但星期日到星期六应该是5。



所以这里是我的算法:


  1. 获取两个日期之间的整天的总数

  2. 除以7以获取整周的数量

  3. 将星期数乘以2,以获得周末天数

  4. 从整体减去周末天数以获得工作日

  5. 如果总天数不是偶数周,则将星期* 7的数字添加到开始日期以获取临时日期

  6. 当临时日期较少时超过结束日期:

    • 如果临时日期不是星期六或星期日,则在工作日添加一个

    • 将一个添加到临时日期


  7. 就是这样。

被其他一些算法所取代,但它永远不会循环超过6天,所以这是一个简单而且合理有效的解决问题的不平衡周。



以上:


  1. 周一至周五为4个工作日

  2. 不同的星期是偶数星期,因此甚至多个5,例如星期一2二月至星期一9月2日和星期日1月2日至星期日8月2日为5个工作日

  3. 星期五6月6日至3月7日7月2日零工作日

  4. 5月6日星期五到星期一9月2日是一个工作日

  5. 星期日2月8日星期日到2月15日星期日2月14日星期四和2月13日星期五全部5个工作日

以下是代码:

  //预期开始日期到结束日期之前
//开始和结束是Date对象
函数dateDifference(开始,结束){

//复制日期对象,因此不要修改原始文件
var s = new Date(+ start);
var e = new Date(+ end);

//设置时间到中午以避免dalight保存和浏览器怪癖
s.setHours(12,0,0,0);
e.setHours(12,0,0,0);

//获取整天的差异
var totalDays = Math.round((e-s)/ 8.64e7);

//获取整周的差额
var wholeWeeks = totalDays / 7 | 0;

//估计工作天数为整周数* 5
var days = wholeWeeks * 5;

//如果没有偶数周数,则剩余周末天数
if(totalDays%7){
s.setDate(s.getDate()+ wholeWeeks * 7) ;

while(s< e){
s.setDate(s.getDate()+ 1);

//如果日不是星期日或星期六,则添加到工作日
if(s.getDay()!= 0&& s.getDay()!= 6 ){
++ days;
}
}
}
返回天数;
}

Dunno如何比较jfriend00的答案或您引用的代码,如果你想期限是包容性的,如果开始或结束日期是工作日,只需添加一个。


I have two sets of codes that work. Needed help combining them into one.

This code gets me the difference between two dates. works perfectly:

function test(){
var date1 = new Date(txtbox_1.value);    
var date2 = new Date(txtbox_2.value);

var diff = (date2 - date1)/1000;
var diff = Math.abs(Math.floor(diff));

var days = Math.floor(diff/(24*60*60));
var leftSec = diff - days * 24*60*60;

var hrs = Math.floor(leftSec/(60*60));
var leftSec = leftSec - hrs * 60*60;

var min = Math.floor(leftSec/(60));
var leftSec = leftSec - min * 60;

txtbox_3.value = days + "." + hrs; }

source for the above code

The code below by @cyberfly appears to have the answer of excluding sat and sun which is what i needed. source. However, its in jquery and the above code is in JS. Therefore, needed help combining as i lacked that knowledge :(

<script type="text/javascript">

$("#startdate, #enddate").change(function() {       

var d1 = $("#startdate").val();
var d2 = $("#enddate").val();

        var minutes = 1000*60;
        var hours = minutes*60;
        var day = hours*24;

        var startdate1 = getDateFromFormat(d1, "d-m-y");
        var enddate1 = getDateFromFormat(d2, "d-m-y");

        var days = calcBusinessDays(new Date(startdate1),new Date(enddate1));             

if(days>0)
{ $("#noofdays").val(days);}
else
{ $("#noofdays").val(0);}


});

</script>


EDIT Made an attempt at combining the codes. here is my sample. getting object expected error.

function test(){
var date1 = new Date(startdate.value);    
var date2 = new Date(enddate.value);
var diff = (date2 - date1)/1000;
var diff = Math.abs(Math.floor(diff)); 
var days = Math.floor(diff/(24*60*60));
var leftSec = diff - days * 24*60*60;
var hrs = Math.floor(leftSec/(60*60));
var leftSec = leftSec - hrs * 60*60;
var min = Math.floor(leftSec/(60));
var leftSec = leftSec - min * 60;

var startdate1 = getDateFromFormat(startdate, "dd/mm/yyyy hh:mm");
var enddate1 = getDateFromFormat(enddate, "dd/mm/yyyy hh:mm");
days = calcBusinessDays(new Date(startdate1),new Date(enddate1));           
noofdays.value = days + "." + hrs; }

start: <input type="text" id="startdate" name="startdate" value="02/03/2015 00:00">
end: <input type="text" id="enddate" name="enddate" value="02/03/2015 00:01">
<input type="text" id="noofdays" name="noofdays" value="">

解决方案

When determining the number of days between two dates, there are lots of decisions to be made about what is a day. For example, the period 1 Feb to 2 Feb is generally one day, so 1 Feb to 1 Feb is zero days.

When adding the complexity of counting only business days, things get a lot tougher. E.g. Monday 2 Feb 2015 to Friday 6 February is 4 elapsed days (Monday to Tuesday is 1, Monday to Wednesday is 2, etc.), however the expression "Monday to Friday" is generally viewed as 5 business days and the duration Mon 2 Feb to Sat 7 Feb should also be 4 business days, but Sunday to Saturday should be 5.

So here's my algorithm:

  1. Get the total number of whole days between the two dates
  2. Divide by 7 to get the number of whole weeks
  3. Multiply the number of weeks by two to get the number of weekend days
  4. Subtract the number of weekend days from the whole to get business days
  5. If the number of total days is not an even number of weeks, add the numbe of weeks * 7 to the start date to get a temp date
  6. While the temp date is less than the end date:
    • if the temp date is not a Saturday or Sunday, add one the business days
    • add one to the temp date
  7. That's it.

The stepping part at the end can probably be replaced by some other algorithm, but it will never loop for more than 6 days so it's a simple and reasonably efficient solution to the issue of uneven weeks.

Some consequences of the above:

  1. Monday to Friday is 4 business days
  2. Any day to the same day in a different week is an even number of weeks and therefore an even mutiple of 5, e.g. Monday 2 Feb to Monday 9 Feb and Sunday 1 Feb to Sunday 8 Feb are 5 business days
  3. Friday 6 Feb to Sunday 7 Feb is zero business days
  4. Friday 6 Feb to Monday 9 Feb is one business day
  5. Sunday 8 Feb to: Sunday 15 Feb, Sat 14 Feb and Fri 13 Feb are all 5 business days

Here's the code:

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

Dunno how it compares to jfriend00's answer or the code you referenced, if you want the period to be inclusive, just add one if the start or end date are a business day.

这篇关于在JavaScript日期计算中排除周末的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆