<p>I have a situation where I want to add hours to a date and have the new date wrap around the work-day. I cobbled up a function to determine this new date, but want to make sure that I'm not forgetting anything.</p>

<p>The hours to be added is called "delay". It could easily be a parameter to the function instead.</p>

<p>Please post any suggestions. [VB.NET Warning]</p>

<pre><code>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 &gt; 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
</code></pre>

<p><strong>Note</strong>: This will probably not work if delay is more than 9 hours long. It should never change from 3, through.</p>

<p>EDIT:
The goal is find the date and time that you get as a result of adding several hours to the current time. This is used to determine a default value for a due date of a submission. I want to add 3 hours to the current time to get the due date time. However, I don't want due dates that go beyond 5pm on the current day. So, I tried to have the hours split between (today, up to 5pm) and (tomorrow, from 8am on), such that adding 3 hours to 4pm would give you 19am, because 1 hour is added to the end of today and 2 hours are added to the beginning of tomorrow.</p>

[{'answer_id': 5303, 'author': 'Mike Stone', 'author_id': 122, 'author_profile': 'https://Stackoverflow.com/users/122', 'pm_score': 2, 'selected': False, 'text': '<p>You should probably write some automated tests for each condition you can think of, and then just start brainstorming more, writing the tests as you think of them.  This way, you can see for sure it will work, and will continue to work if you make further changes.  Look up Test Driven Development if you like the results.</p>\n'}, {'answer_id': 5334, 'author': 'Stu', 'author_id': 414, 'author_profile': 'https://Stackoverflow.com/users/414', 'pm_score': 2, 'selected': False, 'text': '<p>Okay, how about these? The difference between the approaches should speak for themselves.</p>\n\n<p>Also, this is tested about as far as I can throw it. The warranty lasts until... now.</p>\n\n<p>Hope it helps!</p>\n\n<pre><code>Module Module1\n\n    Public Function IsInBusinessHours(ByVal d As Date) As Boolean\n        Return Not (d.Hour &lt; 8 OrElse d.Hour &gt; 17 OrElse d.DayOfWeek = DayOfWeek.Saturday OrElse d.DayOfWeek = DayOfWeek.Sunday)\n    End Function\n\n\n    Public Function AddInBusinessHours(ByVal fromDate As Date, ByVal hours As Integer) As Date\n        Dim work As Date = fromDate.AddHours(hours)\n        While Not IsInBusinessHours(work)\n            work = work.AddHours(1)\n        End While\n        Return work\n    End Function\n\n\n    Public Function LoopInBusinessHours(ByVal fromDate As Date, ByVal hours As Integer) As Date\n        Dim work As Date = fromDate\n        While hours &gt; 0\n            While hours &gt; 0 AndAlso IsInBusinessHours(work)\n                work = work.AddHours(1)\n                hours -= 1\n            End While\n            While Not IsInBusinessHours(work)\n                work = work.AddHours(1)\n            End While\n        End While\n        Return work\n    End Function\n\n    Sub Main()\n        Dim test As Date = New Date(2008, 8, 8, 15, 0, 0)\n        Dim hours As Integer = 5\n        Console.WriteLine("Date: " + test.ToString() + ", " + hours.ToString())\n        Console.WriteLine("Just skipping: " + AddInBusinessHours(test, hours))\n        Console.WriteLine("Looping: " + LoopInBusinessHours(test, hours))\n        Console.ReadLine()\n    End Sub\n\nEnd Module\n</code></pre>\n'}, {'answer_id': 5588, 'author': 'Mike Minutillo', 'author_id': 358, 'author_profile': 'https://Stackoverflow.com/users/358', 'pm_score': 1, 'selected': False, 'text': "<p>I've worked with the following formula (pseudocode) with some success:</p>\n\n<pre><code>now &lt;- number of minutes since the work day started\ndelay &lt;- number of minutes in the delay\nday &lt;- length of a work day in minutes\n\nx &lt;- (now + delay) / day {integer division}\ny &lt;- (now + delay) % day {modulo remainder}\n\nreturn startoftoday + x {in days} + y {in minutes}\n</code></pre>\n"}]
['https://Stackoverflow.com/questions/5260', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/106/']