|
本帖最后由 joforn 于 2013-4-12 23:32 编辑
原来的主函数被我在上传前删除了十来行代码,结果运行结果会出错。这次上传了可以处理2013、2012年节假日的函数,原有Bug已经重新修正。
- Function WordDateDiff(ByVal Date1 As Date, ByVal Date2 As Date, Optional ByVal OutMode As Long = 0) As String
- Dim lngTime As Long, lngDay As Long, DateTemp As Date
-
- If IsLegalHoliday(Date1) Then '如果起始日期是节假日,推后起始日期至节假后第一天的上班时间
- Do While IsLegalHoliday(Date1)
- Date1 = Date1 + 1
- Loop
- Date1 = Format$(Date1, "YYYY-MM-DD") & " 09:00:00"
- Else
- Select Case Hour(Date1) '重新调整起始日期中的时间
- Case Is < 9: Date1 = Format$(Date1, "YYYY-MM-DD") & " 09:00:00"
- Case 12, 13: Date1 = Format$(Date1, "YYYY-MM-DD") & " 12:00:00"
- Case Is > 17: Date1 = Format$(Date1, "YYYY-MM-DD") & " 18:00:00"
- End Select
- End If
-
- If IsLegalHoliday(Date2) Then '如果终止日期是节假日,推后起始日期至节假后第一天的上班时间
- Do While IsLegalHoliday(Date2)
- Date2 = Date2 + 1
- Loop
- Date2 = Format$(Date2, "YYYY-MM-DD") & " 09:00:00"
- Else
- Select Case Hour(Date2) '重新调整终止日期中的时间
- Case Is < 9: Date2 = Format$(Date2, "YYYY-MM-DD") & " 09:00:00"
- Case 12, 13: Date2 = Format$(Date2, "YYYY-MM-DD") & " 14:00:00"
- Case Is > 17: Date2 = Format$(Date2, "YYYY-MM-DD") & " 18:00:00"
- End Select
- End If
-
- If Date2 < Date1 Then '比较调整后的日期是否合法
- WordDateDiff = "Error:错误的日期输入!" '终止日期小于起始日期,返回错误提示
- Else
- lngDay = DateDiff("d", Date1, Date2) - getLegalHoliday(Date1, Date2) '计算相差的天数
-
- '计算出两个工作时间的相差值
- Date1 = CDate(Format$(Date2, "YYYY-MM-DD") & " " & Format$(Date1, "HH:NN:SS"))
- If Date1 > Date2 Then
- lngDay = lngDay - 1
-
- DateTemp = CDate(Format$(Date2, "YYYY-MM-DD") & " 09:00:00")
- lngTime = DateDiff("n", DateTemp, Date2) - IIf(Hour(Date2) > 13, 120, 0)
-
- DateTemp = CDate(Format$(Date2, "YYYY-MM-DD") & " 18:00:00")
- lngTime = lngTime + DateDiff("n", Date1, DateTemp) - IIf(Hour(Date1) < 13, 120, 0)
- Else
- lngTime = lngTime + DateDiff("n", Date1, Date2)
- If (Hour(Date1) < 14) And (Hour(Date2) > 13) Then lngTime = lngTime - 120
- End If
-
- lngDay = lngDay + (lngTime \ 420&)
- lngTime = lngTime Mod 420
- Select Case OutMode '根据OutMode指定的方式输出结果
- Case 1: WordDateDiff = Format$(lngDay, "#00:") & Format$(lngTime \ 60, "#00:") & Format(lngTime Mod 60, "00")
- Case 2: WordDateDiff = Format$(lngDay * 7 + (lngTime \ 60), "#00小时") & Format(lngTime Mod 60, "00分钟")
- Case 3: WordDateDiff = Format$(lngDay, "#00天") & Format$(lngTime \ 60, "#00小时") & Format(lngTime Mod 60, "00分钟")
- Case Else: WordDateDiff = Format$(lngDay * 7 + (lngTime \ 60), "#00:") & Format(lngTime Mod 60, "00")
- End Select
- End If
- End Function
- Private Function getLegalHoliday(ByVal Date1 As Date, ByVal Date2 As Date) As Long
- Dim lngNoWorkDay As Long
- Dim DateTemp As Date
- DateTemp = Date1
- Do While DateTemp < Date2
- If IsLegalHoliday(DateTemp) Then lngNoWorkDay = lngNoWorkDay + 1
- DateTemp = DateTemp + 1
- Loop
- getLegalHoliday = lngNoWorkDay
- End Function
- Private Function IsLegalHoliday(ByVal Date1 As Date) As Boolean
- '本函数目前只能处理2012年及2013年的法定假期,其它年份只判断是否为星期六、星期日。
- '如果有需要,请自己参照下面的格式,添加其它年份的假期表
-
- Date1 = CDate(Format$(Date1, "YYYY-MM-DD"))
- Select Case Year(Date1)
- Case 2012
- Select Case Date1
- Case #1/1/2012# To #1/3/2012#: IsLegalHoliday = True: Exit Function '元旦
- Case #1/22/2012# To #1/28/2012#: IsLegalHoliday = True: Exit Function '春节
- Case #4/2/2012# To #4/4/2012#: IsLegalHoliday = True: Exit Function '清明节
- Case #4/29/2012# To #5/1/2012#: IsLegalHoliday = True: Exit Function '劳动节
- Case #6/22/2012# To #6/24/2012#: IsLegalHoliday = True: Exit Function '端午节
- Case #9/30/2012# To #10/7/2012#: IsLegalHoliday = True: Exit Function '国庆、中秋节
- Case #1/21/2012#, #1/29/2012#, #3/31/2012#, #1/4/2012#, #4/28/2012#, #9/29/2012# '调体上班
- IsLegalHoliday = False: Exit Function
- End Select
- Case 2013
- Select Case Date1
- Case #1/1/2013# To #1/3/2013#: IsLegalHoliday = True: Exit Function '元旦
- Case #2/9/2013# To #2/15/2013#: IsLegalHoliday = True: Exit Function '春节
- Case #4/4/2013# To #4/6/2013#: IsLegalHoliday = True: Exit Function '清明节
- Case #4/29/2013# To #5/1/2013#: IsLegalHoliday = True: Exit Function '劳动节
- Case #6/10/2013# To #6/12/2013#: IsLegalHoliday = True: Exit Function '端午节
- Case #9/19/2013# To #9/21/2013#: IsLegalHoliday = True: Exit Function '中秋节
- Case #10/1/2013# To #10/7/2013#: IsLegalHoliday = True: Exit Function '国庆节
- Case #1/5/2013# To #1/6/2013#, #2/16/2013# To #2/17/2013#, #4/7/2013#, #4/27/2013# To #4/28/2013# _
- , #6/8/2013# To #6/9/2013#, #9/22/2013#, #9/29/2013#, #10/12/2013# '调体上班
- IsLegalHoliday = False: Exit Function
- End Select
- 'Case 2014 '2014年放假表
- Case Else
- End Select
- IsLegalHoliday = Weekday(Date1, vbMonday) > 5
- End Function
复制代码 附上测试文档:
时间函数.rar
(12.79 KB, 下载次数: 19)
|
|