SQL计算时间差并排除周末
CREATE FUNCTION DI_FN_GET_WorkDay (@begin DATETIME , @end DATETIME ) RETURNS int BEGIN DECLARE @i INT , @j INT SET @i = 0 SET @j = 0 IF @end > @begin BEGIN WHILE DATEDIFF(d, DATEADD(d, @i, @begin), @end) <> 0 BEGIN IF DATEPART(weekday, DATEADD(d, @i, @begin)) NOT IN ( 1, 7 ) SET @j = @j + 1 SET @i = @i + 1 END END SELECT @j = ( @j + 1 ) * 24 * 60 - DATEDIFF(Minute, CAST(CONVERT(VARCHAR(10), @begin, 120) AS DATETIME), @begin) - DATEDIFF(Minute, @end, CAST(CONVERT(VARCHAR(10), @end + 1, 120) AS DATETIME)) RETURN @j END
sql 计算结束时间 去除周六周末,
SQL里dateadd计算日期时跳过周六周日两天计算日期
-- 自定义dateadd函数,跳过周六周日. create function [dbo].[dateadd2] (@date0 datetime, @x int) returns datetime as begin declare @i int,@date1 datetime select @i=1,@date1=@date0 while(@i<=@x) begin select @date1=dateadd(d,1,@date1) if datepart(dw,@date1) not in(1,7) begin select @i=@i+1 end end return @date1 end
-- 测试1 select dateadd(D,7,'2015-04-01') 'dateadd',dbo.dateadd2('2015-04-01',7) 'dateadd2' /* dateadd dateadd2 ----------------------- ----------------------- 2015-04-08 00:00:00.000 2015-04-10 00:00:00.000 (1 row(s) affected) */ -- 测试2 select dateadd(D,14,'2015-04-01') 'dateadd',dbo.dateadd2('2015-04-01',14) 'dateadd2' /* dateadd dateadd2 ----------------------- ----------------------- 2015-04-15 00:00:00.000 2015-04-21 00:00:00.000 (1 row(s) affected) */
输入开始时间和工作天数,计算结束时间,需考虑节假日周末和调休
1 <!-- 限制输入周六周末【disabledDays:[0,6]】 且 只能输入今天及今天以后【minDate:'%y-%M-{%d}'】 --> 2 3 <input id="beginTime" class="Wdate" type="text" onFocus="WdatePicker({minDate:'%y-%M-{%d}'})" > 4 <br><br> 5 <input type='text' id="d" onblur="endTime(this.value)"/> 6 <br><br> 7 8 <input type='text' id="endTime" type="text" readonly/> 9 10 11 <script type="text/javascript"> 12 function endTime(day){ 13 //开始时间 14 var beginTime = document.getElementById("beginTime").value; 15 16 document.getElementById("endTime").value = getWorkDayCount(beginTime,day); 17 18 } 19 20 //法定节假日和调休日的设定 21 var Holiday = ["2017-10-1", "2017-10-2", "2017-10-3","2017-10-4","2017-10-5", "2017-10-6", "2017-10-7", "2017-10-8"]; 22 23 //虽然是周末但是还是要上班的【调休】 24 var WeekendsOff = ["2017-9-30"]; 25 26 //“2017-10-09”+12 27 function addDate(dateTemp,days){ 28 29 var dateTemp = dateTemp.split("-"); 30 var nDate = new Date(dateTemp[1] + '-' + dateTemp[2] + '-' + dateTemp[0]); //转换为MM-DD-YYYY格式 31 var millSeconds = Math.abs(nDate) + (days * 24 * 60 * 60 * 1000); 32 var rDate = new Date(millSeconds); 33 var year = rDate.getFullYear(); 34 var month = rDate.getMonth() + 1; 35 if (month.length==1) month = "0" + month; 36 var date = rDate.getDate(); 37 if (date.length==1) date = "0" + date; 38 return (year + "-" + month + "-" + date); 39 } 40 function getWorkDayCount (beginDate, workDay) { 41 /* 42 功能:计算一段时间内工作的天数。不包括周末和法定节假日,法定调休日为工作日,周末为周六、周日两天; 43 参数: 44 beginDate -时间段开始日期; 45 workDay -工作时间; 46 */ 47 var begin = beginDate; 48 var count = 0; 49 50 51 for(var i = 0;i<365;i++){ 52 53 beginDate = addDate(begin,i); 54 var arys1= new Array(); 55 arys1=beginDate.split('-'); //日期为输入日期,格式为 2013-3-10 56 var ssdate=new Date(arys1[0],parseInt(arys1[1]-1),arys1[2]); 57 var day = ssdate.getDay(); 58 59 //1、判断输入的开始时间是不是周末 60 if(day==6 || day==0){ 61 //是周末 不是调休 是节假日 62 if(WeekendsOff.toString().indexOf(beginDate)==-1){ 63 continue; 64 }else{ 65 count++; 66 } 67 68 }else{ 69 if(Holiday.toString().indexOf(beginDate)!=-1){ 70 continue; 71 }else{ 72 count++; 73 } 74 75 } 76 77 78 if(count==workDay) break; 79 80 }; 81 82 //结束时间 83 84 return beginDate; 85 }; 86 87 </script> 88 89 90