【问题标题】:How to determine length of day time and length of night time in a time range?如何确定一个时间范围内的白天时间长度和夜晚时间长度?
【发布时间】:2016-08-25 13:24:26
【问题描述】:

我有两个时间日期,格式为 YYYY-MM-dd hh:mm:ss。

我有一个 24 小时标准的时间范围。 5:30 到 21:00 是白天,21:00 到 5:30 是晚上。

我必须检查这段时间内有多少时间(以秒为单位)是晚上,有多少是白天。

这是一个例子。

我有两个创建句点的日期:

date1 = 2016-08-08 12:31:35(开始)

date2 = 2016-08-09 00:29:11(结束)

从 12:31:35 到 21:00 是 34105 秒,从 21:00:00 到 0:29:11 是 12551 秒。

所以,在我的月经期间,我白天有 34105s,晚上有 12551s。

如果我有例如,我该如何解决? 2016-08-08 12:31:35 到 2016-08-10 12:31:35 什么时候来月经?

【问题讨论】:

    标签: excel vba date


    【解决方案1】:

    VBA 将日期类型存储为浮点数,时间在小数点右侧,日期在小数点左侧。只取小数部分即可得到时间分量:

    time = someDate - Int(someDate)
    

    因此,您可以定义一些有用的常数来计算如何将时间部分分成白天和黑夜:

    Const ONE_SECOND = 1 / 24 / 60 / 60
    Const DAY_START = ONE_SECOND * 60 * 60 * 5.5
    Const DAY_END = ONE_SECOND * 60 * 60 * 21
    Const MIDNIGHT = 1
    Const SECONDS_IN_DAY = DAY_END - DAY_START
    

    然后制作一个简单的辅助函数将 Double 转换为总秒数:

    Public Function TotalSeconds(inValue As Double)
        TotalSeconds = Int(inValue) * 24 * 60 * 60 + _
                       Hour(inValue) * 60 * 60 + _
                       Minute(inValue) * 60 + _
                       Second(inValue)
    End Function
    

    然后,只需确定日期是否相同,并根据开始和结束是否在同一天对 Double 部分进行一些简单的数学运算:

    Private Sub Sample()
        Dim starting As Date, ending As Date
        starting = CDate("2016-08-08 12:31:35")
        ending = CDate("2016-08-09 00:29:11")
    
        Dim days As Long
        'Calculate number of days between dates.
        days = Int(ending) - Int(starting)
        'Remove the date portions
        starting = starting - Int(starting)
        ending = ending - Int(ending)
    
        Dim day As Double, night As Double
        If days = 0 Then  'Same day?
            day = SECONDS_IN_DAY
            If starting < DAY_START Then
                night = night + DAY_START - starting
            Else
                day = day - (starting - DAY_START)
            End If
            If ending > DAY_END Then
                night = night + (ending - DAY_END)
            Else
                day = day - (DAY_END - ending)
            End If
        Else
            'seconds from the first day.
            If starting > DAY_END Then
                night = MIDNIGHT - starting
            Else
                night = MIDNIGHT - DAY_END
                If starting > DAY_START Then
                    day = DAY_END - starting
                Else
                    day = SECONDS_IN_DAY
                    night = night + (DAY_START - starting)
                End If
            End If
            'seconds from the last day.
            If ending > DAY_END Then
                night = night + (ending - SECONDS_IN_DAY)
                day = day + SECONDS_IN_DAY
            Else
                If ending < DAY_START Then
                    night = night + ending
                Else
                    night = night + DAY_START
                    day = day + (ending - DAY_START)
                End If
            End If
            'seconds from full days in between
            If days > 1 Then
                night = night + ((days - 1) * (1 - SECONDS_IN_DAY))
                day = day + ((days - 1) * SECONDS_IN_DAY)
            End If
        End If
    
        Debug.Print "Day: " & TotalSeconds(day), "Night: " & TotalSeconds(night)
    End Sub
    

    【讨论】:

      【解决方案2】:

      我有类似的问题,我使用了不同的解决方案。 我必须为一天中的特定时间启用 3 个表单,其中一个表单是从晚上 11 点到早上 7 点。

      我的解决方案(示例):

      If Not (DateAdd("h", 8, TimeValue(Now)) > DateAdd("h", 8, Sheets(1).Range("B25").Value) And DateAdd("h", 8, TimeValue(Now)) < DateAdd("h", 8, Sheets(1).Range("B25").Value)) Then
      

      基本上,我通过将 8 小时添加到单元格值来进行所有计算,因此我实际上会避免在午夜工作。

      DateAdd("h", 8, TimeValue(Now))
      

      这在某些情况下应该有助于保持代码非常简单和简短。 帮助参考: https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/dateadd-function

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-04-08
        • 2019-11-07
        • 2021-12-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-02
        • 2012-04-10
        相关资源
        最近更新 更多