我有一种情况,我想在一个日期添加小时,并在工作日周围换新日期.我拼凑了一个函数来确定这个新日期,但我想确保我没有忘记任何事情.
要添加的小时称为"延迟".它很容易成为函数的参数.
请发布任何建议.[VB.NET警告]
Private Function GetDateRequired() As Date
''// A decimal representation of the current hour
Dim hours As Decimal = Decimal.Parse(Date.Now.Hour) + (Decimal.Parse(Date.Now.Minute) / 60.0)
Dim delay As Decimal = 3.0 ''// delay in hours
Dim endOfDay As Decimal = 12.0 + 5.0 ''// end of day, in hours
Dim startOfDay As Decimal = 8.0 ''// start of day, in hours
Dim newHour As Integer
Dim newMinute As Integer
Dim dateRequired As Date = Now
Dim delta As Decimal = hours + delay
''// Wrap around to the next day, if necessary
If delta > endOfDay Then
delta = delta - endOfDay
dateRequired = dateRequired.AddDays(1)
newHour = Integer.Parse(Decimal.Truncate(delta))
newMinute = Integer.Parse(Decimal.Truncate((delta - newHour) * 60))
newHour = startOfDay + newHour
Else
newHour = Integer.Parse(Decimal.Truncate(delta))
newMinute = Integer.Parse(Decimal.Truncate((delta - newHour) * 60))
End If
dateRequired = New Date(dateRequired.Year, dateRequired.Month, dateRequired.Day, newHour, newMinute, 0)
Return dateRequired
End Sub
Run Code Online (Sandbox Code Playgroud)
注意:如果延迟超过9小时,这可能不起作用.它永远不应该从3变为.
编辑:目标是找到由于在当前时间增加几个小时而获得的日期和时间.这用于确定提交截止日期的默认值.我想在当前时间增加3个小时以获得到期日期.但是,我不希望截止日期超过当天下午5点.因此,我试图把时间分成(今天,直到下午5点)和(明天,从早上8点开始),这样加上3个小时到下午4点会给你19点,因为1小时被添加到今天结束,2小时被添加到明天开始.
您可能应该为您能想到的每种情况编写一些自动化测试,然后开始更多的头脑风暴,根据您的想法编写测试。这样,您可以确定它会起作用,并且如果您进行进一步的更改,它将继续起作用。如果您喜欢结果,请查找测试驱动开发。
归档时间: |
|
查看次数: |
1071 次 |
最近记录: |