向日期添加工作日





4.00/5 (2投票s)
如何向开始日期添加一定数量的工作日

引言
你是否曾经需要向日期添加天数,然后确保这些天数都是工作日?
我确实遇到过很多次这种情况,当一位初级开发人员问我如何处理时,我想与大家分享一下。这只适用于标准工作周。也就是说,周一
到周五
。周六
和周日
将被视为周末。代码很容易理解,如果需要,应该很容易修改以适应你自己的特定用途。
Using the Code
要简单地向起始日期添加 2 个工作周,你可以调用函数 CalculateTenBusinessDaysFromInputDate
并将 StartDate
传递给它。该函数将返回一个距离 StartDate
有 10 个工作日的日期。如果 Startdate
是 周六
,我们只需向 StartDate
传递的值添加 16 天。如果 StartDate
是 周日
,则向 StartDate
添加 15 天。对于一周的任何其他日期,我们只需向 StartDate
添加 14 天。
VB.NET
Public Function CalculateTenBusinessDaysFromInputDate(ByVal StartDate As Date) As Date
'This simply adds at least 2 full weeks to the start date.
Select Case StartDate.DayOfWeek
Case DayOfWeek.Sunday
'if the start date is not a sunday you need to add
'1 day to push it to a monday that is why the number is 15.
Return StartDate.AddDays(15)
Case DayOfWeek.Monday, DayOfWeek.Tuesday, _
DayOfWeek.Wednesday, DayOfWeek.Thursday, DayOfWeek.Friday
'if the start date is any other day then just add 14 days to the start date.
Return StartDate.AddDays(14)
Case DayOfWeek.Saturday
'if the start date is on a Saturday you need to add
'2 days to push it to a monday that is why the number is 16.
Return StartDate.AddDays(16)
Case Else
Return StartDate
End Select
End Function
C#
public System.DateTime CalculateTenBusinessDaysFromInputDate(System.DateTime StartDate)
{
//This simply adds at least 2 full weeks to the start date.
switch (StartDate.DayOfWeek)
{
case DayOfWeek.Sunday:
//if the start date is not a sunday you need to add
//1 day to push it to a monday that is why the number is 15.
return StartDate.AddDays(15);
break;
case DayOfWeek.Monday:
case DayOfWeek.Tuesday:
case DayOfWeek.Wednesday:
case DayOfWeek.Thursday:
case DayOfWeek.Friday:
//if the start date is any other day then just add 14 days to the start date.
return StartDate.AddDays(14);
break;
case DayOfWeek.Saturday:
//if the start date is on a Saturday you need to add
//2 days to push it to a monday that is why the number is 16.
return StartDate.AddDays(16);
break;
default:
return StartDate;
}
}
这段代码更具现实意义。要使用 CalculateBusinessDaysFromInputDate
函数,你需要提供 StartDate
以及要添加到 StartDate
的工作日数量。在 CalculateBusinessDaysFromInputDate
中,我们首先检查 StartDate
是否为 周六
或 周日
。如果是,则我将工作日数量减少 1。我这样做是因为我们不希望结果日期是 11 个工作日之后。注意:我没有在 CalculateTenBusinessDaysFromInputDate
中这样做。 例如,如果 StartDate
是 周一
,并且我们向其添加 12 个工作日,那么结果应该在星期三。在返回结果日期之前,我们需要确保该日期不是周末。如果是,我会将日期移动到下一个 周一
。你也可以通过简单地使用 .AddDays() Date
函数并传递一个负数来将日期移回之前的 周五
。例如 .AddDays(-2)
。
VB.NET
Public Function CalculateBusinessDaysFromInputDate_
(ByVal StartDate As Date, ByVal NumberOfBusinessDays As Integer) As Date
'Knock the start date down one day if it is on a weekend.
If StartDate.DayOfWeek = DayOfWeek.Saturday Or StartDate.DayOfWeek = _
DayOfWeek.Sunday Then
NumberOfBusinessDays -= 1
End If
For index = 1 To NumberOfBusinessDays
Select Case StartDate.DayOfWeek
Case DayOfWeek.Sunday
StartDate = StartDate.AddDays(2)
Case DayOfWeek.Monday, DayOfWeek.Tuesday, DayOfWeek.Wednesday, _
DayOfWeek.Thursday, DayOfWeek.Friday
StartDate = StartDate.AddDays(1)
Case DayOfWeek.Saturday
StartDate = StartDate.AddDays(3)
End Select
Next
'check to see if the end date is on a weekend.
'If so move it ahead to Monday.
'You could also bump it back to the Friday before if you desired to.
'Just change the code to -2 and -1.
If StartDate.DayOfWeek = DayOfWeek.Saturday Then
StartDate = StartDate.AddDays(2)
ElseIf StartDate.DayOfWeek = DayOfWeek.Sunday Then
StartDate = StartDate.AddDays(1)
End If
Return StartDate
End Function
C#
public System.DateTime CalculateBusinessDaysFromInputDate
(System.DateTime StartDate, int NumberOfBusinessDays)
{
//Knock the start date down one day if it is on a weekend.
if (StartDate.DayOfWeek == DayOfWeek.Saturday |
StartDate.DayOfWeek == DayOfWeek.Sunday)
{
NumberOfBusinessDays -= 1;
}
int index = 0;
for (index = 1; index <= NumberOfBusinessDays; index++)
{
switch (StartDate.DayOfWeek)
{
case DayOfWeek.Sunday:
StartDate = StartDate.AddDays(2);
break;
case DayOfWeek.Monday:
case DayOfWeek.Tuesday:
case DayOfWeek.Wednesday:
case DayOfWeek.Thursday:
case DayOfWeek.Friday:
StartDate = StartDate.AddDays(1);
break;
case DayOfWeek.Saturday:
StartDate = StartDate.AddDays(3);
break;
}
}
//check to see if the end date is on a weekend.
//If so move it ahead to Monday.
//You could also bump it back to the Friday before if you desired to.
//Just change the code to -2 and -1.
if (StartDate.DayOfWeek == DayOfWeek.Saturday)
{
StartDate = StartDate.AddDays(2);
}
else if (StartDate.DayOfWeek == DayOfWeek.Sunday)
{
StartDate = StartDate.AddDays(1);
}
return StartDate;
}
关注点
这是一种非常简单的计算工作日的方法。它没有复杂的算法。
如果你需要计算过去的天数,这也可以正常工作。希望这在某种程度上有所帮助。
历史
- 2009 年 10 月 28 日:初始版本