在工作日期间最好的方法是什么? [英] What is the best way to wrap time around the work day?

查看:224
本文介绍了在工作日期间最好的方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个情况,我想加上几个小时到一个日期,并在工作日围绕新的日期。我拼凑了一个功能来确定这个新的日期,但是想确保我不会忘记任何东西。

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.

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

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

请发布任何建议。 [VB.NET Warning]

Please post any suggestions. [VB.NET Warning]

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改变。

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

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

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.

推荐答案

方法之间的区别应该自己说明。

Okay, how about these? The difference between the approaches should speak for themselves.

此外,这个测试是我可以抛出的。保证期至...现在。

Also, this is tested about as far as I can throw it. The warranty lasts until... now.

希望有帮助!

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

这篇关于在工作日期间最好的方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆