时间经过午夜时的DateTime范围问题 [英] DateTime range issue when time passes midnight

查看:52
本文介绍了时间经过午夜时的DateTime范围问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做工作申请,我刚好遇到障碍。

I'm doing an application for work and I've just ran into a brickwall.

我们有3个班次,06:00-13:36,13 :36-23:00和23:00-06:00。
我有两个标签,一个使用以下代码显示轮班开始时间,另一个显示轮班结束时间:

We have 3 shifts, 06:00-13:36, 13:36-23:00 and 23:00-06:00. I have two labels, one to display shift start time and the other shift end time using the following code:

    Dim StartTimeN As Date = DateTime.Now.AddDays(-1).ToShortDateString & " 23:01"
    Dim EndTimeN As Date = DateTime.Now.ToShortDateString & " 06:00"
    Dim StartTimeF As Date = DateTime.Now.ToShortDateString & " 06:01"
    Dim EndTimeF As Date = DateTime.Now.ToShortDateString & " 13:36"
    Dim StartTimeE As Date = DateTime.Now.ToShortDateString & " 13:37"
    Dim EndTimeE As Date = DateTime.Now.ToShortDateString & " 23:00"
    Dim CurrentTime As Date = DateTime.Now


    If (CurrentTime.Ticks >= StartTimeN.Ticks And CurrentTime.Ticks <= EndTimeN.Ticks) Then
        Label1.Text = "23:00"
        Label2.Text = "06:00"
    ElseIf (CurrentTime.Ticks >= StartTimeF.Ticks And CurrentTime.Ticks <= EndTimeF.Ticks) Then
        Label1.Text = "06:00"
        Label2.Text = "13:36"
    ElseIf (CurrentTime.Ticks >= StartTimeE.Ticks And CurrentTime.Ticks <= EndTimeE.Ticks) Then
        Label1.Text = "13:36"
        Label2.Text = "23:00"
    End If

除夜班外,代码工作正常。我无法在23-00之间设置adddays(-1),因为这会弄乱SQL中的数据收集。
我有一张图表,收集轮班之间的sql数据(例如06:00-13:36等)。
因为AddDays(-1)会收集前一天的数据,所以这会在23:00到23:59之间的一个小时内产生问题。 SQL选择SUM查询在DateTime.Now.ToShortDateString&之间使用 label1data(shiftstart)和DateTime.Now

Code works fine, except for the nightshift. I can't have adddays(-1) between 23-00 since that messes up the datacollection from SQL. I have a chart that collects sql data between the shiftours (eg. 06:00-13:36 etc). This creates an issue for the hour between 23:00 and 23:59 since the AddDays(-1) collects the data from the previous day then. Sql select SUM query uses between DateTime.Now.ToShortDateString & " label1data(shiftstart)" and DateTime.Now

我不确定如何解决23到00之间的小时。我是否需要创建一个IF语句那个小时,还有一个特定的SQL函数,然后将23:00-00:00和00:00-06:00的结果加在一起,还是有更简单的方法呢?

I'm not sure how to solve the hour between 23 and 00. Do I need to create a single IF statement for that hour and also a specific SQL function and then add together the results from 23:00-00:00 and 00:00-06:00 or is there an easier way of doing this?

我是一个初学者,对于答案很简单,我感到抱歉,但是这个问题几乎导致我放弃应用程序,因为我无法正常使用它。

I'm a beginner and I'm sorry if the answer is a simple one, but this issue has almost led me to the point of giving up on the application since I can't get it to work properly.

同样基于shifthours的其他函数正在计算shiftstart和DateTime之间的TimeSpan。现在,为了获得到目前为止在shift中经过的小时数。然后将其乘以每小时最大产品数,以显示如果以最大产能运行可能会生产多少产品。整个应用程序的建立是基于向操作员显示他们在班次期间的生产信息。

Other functions also based on shifthours is calculating the TimeSpan between shiftstart and DateTime.Now in order to get amount of hours that has passed during the shift so far. Then multiplying that with maxproducts per hour in order to display how many products that could have been produced if ran at max capacity. The entire application is build upon displaying information for the operators about their production during their shift.

感谢您的协助。

推荐答案

您的问题是夜班结束了两天,因此您无法以相同的方式进行检查。

Your issue is the fact that a night shift falls over two days, so you can't check it in the same way.

这是为您提供的解决方案。它可能看起来很复杂,但是它是改进的结构,并且易于维护。我使用时间跨度而不是日期,因为我认为这更适合此解决方案,因为您不必担心日期部分

here is a solution for you. It may look complicated but it is an improved structure and will be easier to maintain. I have used timespans rather than dates as I think this lends itself better to this solution because you don't have to worry about the date portion

''' <summary>
''' A simple class to hold a shift start and end times
''' </summary>
Public Class Shift
    Public Property StartTime As TimeSpan
    Public Property EndTime As TimeSpan
    Public ReadOnly Property IsnightShift As Boolean
        Get
            Return EndTime < StartTime
        End Get
    End Property
    Public Sub New(startTime As TimeSpan, endTime As TimeSpan)
        Me.StartTime = startTime
        Me.EndTime = endTime
    End Sub
End Class

然后从班次列表中查找班次:

Then a function to find a shift from a list of shifts:

''' <summary>
''' Finds the shift for a given time
''' </summary>
''' <param name="shifts"></param>
''' <param name="timeNow"></param>
''' <returns></returns>
Public Function FindShiftStartAndEndTimes(shifts As List(Of Shift), timeNow As TimeSpan) As Shift
    Dim endOfDayTime As New TimeSpan(24, 0, 0)
    Dim startOfDay As New TimeSpan(0, 0, 0)

    For Each shift In shifts
        'nigh shift is a special case so check we are after the start time and before midnight OR after midnight on the next day and before the end of shift
        If shift.IsnightShift Then
            If (timeNow >= shift.StartTime AndAlso timeNow < endOfDayTime) OrElse 
               (timeNow >= startOfDay AndAlso timeNow < shift.EndTime) Then
                Return shift
            End If
        Else
            If timeNow >= shift.StartTime AndAlso timeNow < shift.EndTime Then
                Return shift
            End If
        End If
    Next
    Throw New Exception("Could not find a shift for that time")
End Function

然后,您可以找到当前的班次:

Then you can find the current shift like this:

Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
    'create a list of shifts
    Dim shifts As New List(Of Shift)
    'add your shifts to the list
    shifts.Add(New Shift(New TimeSpan(6, 0, 0), New TimeSpan(13, 36, 0)))
    shifts.Add(New Shift(New TimeSpan(13, 36, 0), New TimeSpan(23, 0, 0)))
    shifts.Add(New Shift(New TimeSpan(23, 0, 0), New TimeSpan(6, 0, 0)))

    'find the current shift
    Dim shift = FindShiftStartAndEndTimes(shifts, New TimeSpan(DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second))
    MessageBox.Show("Current Shift is " & shift.StartTime.ToString & "-" & shift.EndTime.ToString)
End Sub

这篇关于时间经过午夜时的DateTime范围问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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