在工作日结束时间的最佳方法是什么?

时间:2008-08-07 20:21:52

标签: vb.net date

我有一种情况,我希望在某个日期添加小时数,并在工作日期间包含新日期。我拼凑了一个函数来确定这个新日期,但是想确保我没有忘记任何事情。

要添加的小时数称为“延迟”。它可以很容易地成为函数的参数。

请发布任何建议。 [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

注意:如果延迟超过9小时,这可能不起作用。它永远不应该从3变为。

编辑: 目标是找到由于在当前时间增加几个小时而获得的日期和时间。这用于确定提交截止日期的默认值。我想在当前时间增加3个小时以获得到期日期。但是,我不希望截止日期超过当天下午5点。因此,我试图把时间分成(今天,直到下午5点)和(明天,从早上8点开始),这样加上3个小时到下午4点会给你19点,因为1小时被添加到今天结束,2小时被添加到明天开始。

3 个答案:

答案 0 :(得分:3)

好的,这些怎么样?这些方法之间的区别应该说明一切。

此外,这是我可以抛出的测试。保修期一直持续到现在。

希望它有所帮助!

Module Module1

    Public Function IsInBusinessHours(ByVal d As Date) As Boolean
        Return Not (d.Hour < 8 OrElse d.Hour > 17 OrElse d.DayOfWeek = DayOfWeek.Saturday OrElse d.DayOfWeek = DayOfWeek.Sunday)
    End Function


    Public Function AddInBusinessHours(ByVal fromDate As Date, ByVal hours As Integer) As Date
        Dim work As Date = fromDate.AddHours(hours)
        While Not IsInBusinessHours(work)
            work = work.AddHours(1)
        End While
        Return work
    End Function


    Public Function LoopInBusinessHours(ByVal fromDate As Date, ByVal hours As Integer) As Date
        Dim work As Date = fromDate
        While hours > 0
            While hours > 0 AndAlso IsInBusinessHours(work)
                work = work.AddHours(1)
                hours -= 1
            End While
            While Not IsInBusinessHours(work)
                work = work.AddHours(1)
            End While
        End While
        Return work
    End Function

    Sub Main()
        Dim test As Date = New Date(2008, 8, 8, 15, 0, 0)
        Dim hours As Integer = 5
        Console.WriteLine("Date: " + test.ToString() + ", " + hours.ToString())
        Console.WriteLine("Just skipping: " + AddInBusinessHours(test, hours))
        Console.WriteLine("Looping: " + LoopInBusinessHours(test, hours))
        Console.ReadLine()
    End Sub

End Module

答案 1 :(得分:2)

您应该为您能想到的每种情况编写一些自动化测试,然后开始集思广益,按照您的想法编写测试。这样,您可以确定它将起作用,并且如果您进行进一步更改,它将继续有效。如果您喜欢结果,请查看测试驱动开发。

答案 2 :(得分:1)

我使用了以下公式(伪代码)并取得了一些成功:

now <- number of minutes since the work day started
delay <- number of minutes in the delay
day <- length of a work day in minutes

x <- (now + delay) / day {integer division}
y <- (now + delay) % day {modulo remainder}

return startoftoday + x {in days} + y {in minutes}