我有3个from package import *
,一个用于设置日期,第二个用于添加3天,第三个用于添加5个工作日。但是我的代码没有正确计算。
我计算“手动”未来几天增加或减少当天的日子。
DateTimePicker
答案 0 :(得分:2)
以下是一种扩展方法,您可以调用AddDays
:
Imports System.Runtime.CompilerServices
Public Module DateTimeExtensions
<Extension>
Public Function AddWeekDays(source As Date, value As Integer) As Date
Dim result = source
Do
result = result.AddDays(1)
If result.IsWeekDay() Then
value -= 1
End If
Loop Until value = 0
Return result
End Function
<Extension>
Public Function IsWeekDay(source As Date) As Boolean
Return source.DayOfWeek <> DayOfWeek.Saturday AndAlso
source.DayOfWeek <> DayOfWeek.Sunday
End Function
End Module
然后你可以这样称呼:
DateTimePicker3.Value = DateTimePicker1.Value.AddWeekDays(latedlvy)
请注意,与Date.AddDays
不同,该方法需要Integer
而不是Double
。此外,它只适用于正值。可以改进它以与AddDays
完全相同的方式工作,但在这种情况下你可能不需要它。
如果您不确定扩展方法的工作原理,我建议您对该主题进行一些阅读。
编辑:我已经对此方法做了一些工作并对其进行了大幅改进。它现在处理负值和小数值,就像Date.AddDays
一样。
Imports System.Runtime.CompilerServices
''' <summary>
''' Contains methods that extend the <see cref="DateTime"/> structure.
''' </summary>
Public Module DateTimeExtensions
''' <summary>
''' Gets a value indicating whether a <see cref="DateTime"/> value represents a week day.
''' </summary>
''' <param name="source">
''' The input <see cref="DateTime"/>, which acts as the <b>this</b> instance for the extension method.
''' </param>
''' <returns>
''' <b>true</b> if the represents a week day; otherwise <b>false</b>.
''' </returns>
''' <remarks>
''' All days other than Saturday and Sunday are considered week days.
''' </remarks>
<Extension>
Public Function IsWeekDay(source As Date) As Boolean
Return source.DayOfWeek <> DayOfWeek.Saturday AndAlso
source.DayOfWeek <> DayOfWeek.Sunday
End Function
''' <summary>
''' Returns a new <see cref="DateTime"/> that adds the specified number of week days to a specified value.
''' </summary>
''' <param name="source">
''' The input <see cref="DateTime"/>, which acts as the <b>this</b> instance for the extension method.
''' </param>
''' <param name="value">
''' A number of whole and fractional days. The <i>value</i> parameter can be negative or positive.
''' </param>
''' <returns>
''' An object whose value is the sum of the date and time represented by this instance and the number of week days represented by <i>value</i>.
''' </returns>
''' <remarks>
''' All days other than Saturday and Sunday are considered week days.
''' </remarks>
<Extension>
Public Function AddWeekDays(source As Date, value As Double) As Date
'A unit will be +/- 1 day.
Dim unit = Math.Sign(value) * 1.0
'Start increasing the date by units from the initial date.
Dim result = source
'When testing for zero, allow a margin for precision error.
Do Until Math.Abs(value) < 0.00001
If Math.Abs(value) < 1.0 Then
'There is less than one full day to add so we need to see whether adding it will take us past midnight.
Dim temp = result.AddDays(value)
If temp.Date = result.Date OrElse temp.IsWeekDay() Then
'Adding the partial day did not take us into a weekend day so we're done.
result = temp
value = 0.0
Else
'Adding the partial day took us into a weekend day so we need to add another day.
result = result.AddDays(unit)
End If
Else
'Add a single day.
result = result.AddDays(unit)
If result.IsWeekDay() Then
'Adding a day did not take us into a weekend day so we can reduce the remaining value to add.
value -= unit
End If
End If
Loop
Return result
End Function
End Module
答案 1 :(得分:0)
这是另一种方法。我认为这是相当直接的 - 特别是如果你只做了几次计算。
Dim someDate As DateTime = DateTime.Now.Date
Dim dates = _
Enumerable _
.Range(1, 100000) _
.Select(Function (x) someDate.AddDays(x)) _
.Where(Function (x) x.DayOfWeek <> DayOfWeek.Saturday) _
.Where(Function (x) x.DayOfWeek <> DayOfWeek.Sunday)
Dim nextDate = dates.Take(5).First()
这种方法的一大优势是您还可以添加更多.Where
次来取消公众假期。
简单。