取得周末之前的两个日期之间的天数 [英] Get number of days between two Dates excluding weekends

查看:143
本文介绍了取得周末之前的两个日期之间的天数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码减去两个日期

I subtract two dates using below code

Dim OpenDate, ClosedDate As DateTime
dim Result as String
OpenDate = Convert.ToDateTime(some string contain date and time)
ClosedDate = Convert.ToDateTime(some string contain date and time)
Result = ClosedDate.Subtract(OpenDate).ToString

它的格式正确结果正常(day.hour:minute:second)

it works fine with correct result in format (day.hour:minute:second )

我需要的是

如果关闭和开放日期之间的周期包含星期五或星期六从结果中减去

if period between closed and opened date contain Friday or Saturday subtract from result

例如果结果9.03:02:10包含2星期六和星期五它必须是6.03:02:10等等

Ex. if result 9.03:02:10 contain 2 Saturday and Friday it must be 6.03:02:10 and so on

推荐答案

但可读的方法:

Public Shared Function GetBusinessDays(startDay As DateTime, endDay As DateTime) As Integer
    Dim today = Date.Today
    Dim weekend = {DayOfWeek.Saturday, DayOfWeek.Sunday}
    Dim businessDays = 
        From d In Enumerable.Range(0, (endDay.Date - startDay.Date).Days + 1)
        Select day = today.AddDays(d)
        Where Not weekend.Contains(day.DayOfWeek)
    Return businessDays.Count()
End Function

测试:

Dim days As Int32 = GetBusinessDays(Date.Now, Date.Now.AddDays(9))
Console.Write(days) ' 7

LINQ查询首先创建从0到天的整数范围(+1,因为包括最后一天)。然后通过 today.AddDays(days)创建 Date 对象。由于周末 DayOfWeek 的数组,您可以使用 Enumerable.Contains 只采取不是周末日期的日期。最后一步是使用 Enumerable.Count 来执行查询,以获取工作天数。

The LINQ query first creates a range of integers from 0 to days (+1 because including last day). Then it creates Date objects via today.AddDays(days). Since weekend is an array of DayOfWeek you can use Enumerable.Contains to take only dates which are not weekend-dates. The last step is to execute the query by using Enumerable.Count to get the number of business days.

你可以通过提供 ParamArray 用于银行假期:

You could improve it by providing a ParamArray for the banking-holidays:

Public Shared Function GetBusinessDays(startDay As DateTime, endDay As DateTime, ParamArray bankHoliday As Date()) As Integer
    Dim today = Date.Today
    Dim nonWorkingDays = New HashSet(Of Date)(bankHoliday)
    Dim weekend = {DayOfWeek.Saturday, DayOfWeek.Sunday}
    Dim businessDays =
        From d In Enumerable.Range(0, (endDay - startDay).Days + 1)
        Select day = today.AddDays(d)
        Where Not weekend.Contains(day.DayOfWeek) AndAlso Not nonWorkingDays.Contains(day)
    Return businessDays.Count()
End Function

尽管如此,有银行假日。如果你有一个或多个,你可以传递一个真正的数组作为参数或单个对象像这样圣诞节:

That works as it is even if you don't have bank holiday days. If you have one or multiple you can either pass a real array as argument or single objects like here for christmas:

Dim christmas = New Date(2014, 12, 25)
Dim days As Int32 = GetBusinessDays(Date.Now, Date.Now.AddDays(9), christmas)

或几个单一对象:

Dim christmasEve = New Date(2014, 12, 24)
Dim days As Int32 = GetBusinessDays(Date.Now, Date.Now.AddDays(9), christmasEve, christmas)

这篇关于取得周末之前的两个日期之间的天数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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