【问题标题】:How can I account for the midnight time change in this code?如何解释此代码中的午夜时间变化?
【发布时间】:2016-06-03 15:49:17
【问题描述】:

我需要为 3 种不同类型的班次定义班次长度:8 小时、10 小时或 12 小时班次。

8 小时轮班:

第一班:早上 6 点到下午 2 点

第二班:2pm-10pm

第三班:晚上 10 点至早上 6 点

10 小时轮班:

第一班:早上 5 点到下午 3 点

第二班:下午 3 点到凌晨 1 点

12 小时轮班:

第一班:凌晨 3 点至下午 3 点

第二班:下午 3 点到凌晨 3 点

问题是我不确定处理午夜转换的最佳方法是什么。当您执行Datediff("n", #10:00:00:pm#, #06:00:00#) 时,它会给出一个负值,因为它认为您想要从早上 6 点到晚上 10 点的时间差(但为负值)。这不是我想要的。

我下面的代码在午夜线变化的时间一直给我负值。

这里极其棘手的部分是用户可以提交start_time,比如说,凌晨 2:30:00,这是过去午夜线。这使事情进一步复杂化。

我怎样才能使我的代码不仅返回正确的班次长度,而且还考虑到用户在午夜之后提交开始时间的可能性?

(Switch
    (Max([dbo_job.Uf_Shift_Standard])="8",
        (Switch(
            (tbl_OEE.shift = "SH1"),
                Datediff("n", Max([start_time]), [end_time]),
            (tbl_OEE.shift = "SH2"),
                Datediff("n", Max([start_time]), [end_time]),
            True,
                Datediff("n", Max([start_time]), #23:59:59#) + Datediff("n", #00:00:00#, #06:00:00#))),
    Max([dbo_job.Uf_Shift_Standard])="10",
        (Switch(
            (tbl_OEE.shift = "SH1"),
                Datediff("n", Max([start_time]), [end_time]),
            True,
                Datediff("n", Max([start_time]), #23:59:59#) + Datediff("n", #00:00:00#, #01:00:00#))),
    Max([dbo_job.Uf_Shift_Standard])="12",
        (Switch(
            (tbl_OEE.shift = "SH1"),
                Datediff("n", Max([start_time]), #15:00:00#),
            True,
                Datediff("n", Max([start_time]), #23:59:59#) + Datediff("n", #00:00:00#, #03:00:00#)))
    )   
) AS actual_shift_length_minutes

【问题讨论】:

    标签: sql ms-access time


    【解决方案1】:

    可以将时差加1(=一天),然后直接用TimeValue计算,去掉日期部分:

    ShiftDuration = TimeValue(CDate(#06:00:00 am# - #10:00:00 pm# + 1))
    ShiftDuration = TimeValue(CDate(#10:00:00 pm# - #02:00:00 pm# + 1))
    ShiftDuration = TimeValue(CDate(#02:00:00 pm# - #06:00:00 am# + 1))
    

    所有这些都将返回 08:00:00 的日期/时间值(时间跨度),可以根据需要使用 Format 格式化显示或转换为小时和/或分钟。

    如果您需要显示超过 24 小时的持续时间,请使用如下函数:

    Public Function FormatHourMinute( _
      ByVal datTime As Date, _
      Optional ByVal strSeparator As String = ":") _
      As String
    
    ' Returns count of days, hours and minutes of datTime
    ' converted to hours and minutes as a formatted string
    ' with an optional choice of time separator.
    '
    ' Example:
    '   datTime: #10:03# + #20:01#
    '   returns: 30:04
    '
    ' 2005-02-05. Cactus Data ApS, CPH.
    
      Dim strHour       As String
      Dim strMinute     As String
      Dim strHourMinute As String
    
      strHour = CStr(Fix(datTime) * 24 + Hour(datTime))
      ' Add leading zero to minute count when needed.
      strMinute = Right("0" & CStr(Minute(datTime)), 2)
      strHourMinute = strHour & strSeparator & strMinute
    
      FormatHourMinute = strHourMinute
    
    End Function
    

    【讨论】:

      猜你喜欢
      • 2017-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-20
      • 2020-02-28
      • 2016-01-13
      相关资源
      最近更新 更多