时间午夜过后的日期时间范围问题

时间:2016-07-19 05:36:58

标签: .net vb.net datetime timespan

我正在做一个工作申请,而我刚刚遇到了砖墙。

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

    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中的数据收集。 我有一个图表,收集shiftours之间的SQL数据(例如06:00-13:36等)。 这会产生23:00到23:59之间的小时问题,因为AddDays(-1)会收集前一天的数据。 Sql选择SUM查询在DateTime.Now.ToShortDateString&amp;之间使用。 &#34; label1data(shiftstart)&#34;和DateTime.Now

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

我是初学者,如果答案很简单,我很抱歉,但是这个问题几乎让我放弃了应用程序,因为我无法得到它工作正常。

同样基于shifthours的其他函数正在计算shiftstart和DateTime.Now之间的TimeSpan,以便获得到目前为止在班次期间经过的小时数。然后将其与每小时的maxproducts相乘,以显示在最大容量下运行时可生成的产品数量。整个应用程序的基础是为操作员显示他们在轮班期间的生产信息。

感谢任何帮助。

2 个答案:

答案 0 :(得分:0)

是的,您需要根据具体情况确定使​​用时间:

Dim StartTimeN As Date
Dim EndTimeN As Date

If (DateTime.Now.Hour = 23) Then
    StartTimeN = DateTime.Now.ToShortDateString & " 23:01"
    EndTimeN = DateTime.Now.AddDays(1).ToShortDateString & " 06:00"
Else
    StartTimeN = DateTime.Now.AddDays(-1).ToShortDateString & " 23:01"
    EndTimeN = DateTime.Now.ToShortDateString & " 06:00"
End

答案 1 :(得分:0)

您的问题是夜班超过两天,因此您无法以同样的方式检查。

这是一个适合您的解决方案。它可能看起来很复杂,但它是一种改进的结构,更容易维护。我使用过时间而不是日期,因为我认为这样可以更好地解决这个问题,因为你不必担心日期部分

''' <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

然后是一个从班次列表中找到班次的函数:

''' <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

然后你可以找到这样的当前转变:

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