如何确定时间范围内的白天时间和夜间时间? [英] How to determine length of day time and length of night time in a time range?
问题描述
我有两个带YYYY-MM-dd hh:mm:ss格式的日期。
I have two dates with time in YYYY-MM-dd hh:mm:ss format.
我的时间范围为24H标准。从5:30到21:00是白天,从21:00到5:30是晚上。
I have a time range in 24H standard. From 5:30 to 21:00 is day and from 21:00 to 5:30 is night.
我必须检查这段时间有多少时间(以秒为单位)是晚上,白天是多少。
I must check how much time (in seconds) in this period was night and how much was day.
这里是一个例子。
我有两个约会,时间:
date1 = 2016-08-08 12:31:35(开始)
date1 = 2016-08-08 12:31:35 (start)
date2 = 2016 -08-09 00:29:11(结束)
date2 = 2016-08-09 00:29:11 (end)
从12:31:35到21:00是34105s和从21:00:00到0:29 :11是12551s。
From 12:31:35 to 21:00 is 34105s and from 21:00:00 to 0:29:11 is 12551s.
因此,在我的时期内,我白天有34105s,晚上有12551s。
So, in my period I have 34105s during the day and 12551s during the night.
当我有例如从2016-08-08 12:31:35到2016-08-10 12:31:35当我有两天的时间?
How can I solve when I have e.g. from 2016-08-08 12:31:35 to 2016-08-10 12:31:35 when I have two days in my period?
推荐答案
我遇到了类似的问题,并且使用了不同的解决方案。
我必须在一天中的特定时间启用3个表单,并且其中一个表单是从晚上11点到上午7点。
I had similar issue and I have used different solution. I had to have 3 forms enabled for specific times during the day and one of the forms would be from 11pm to 7am.
我的解决方案(示例):
My solution (example):
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小时来进行所有计算的,我实际上会避免在午夜时间工作。
Basically I was making all calculations based on cell values by adding 8 hours to them so I would actually avoid working over midnight hours.
DateAdd("h", 8, TimeValue(Now))
在某些情况下,这应该有助于使代码非常简单和简短。
帮助参考:
https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/dateadd-function
This should help in some cases keeping code very simple and short. Help reference: https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/dateadd-function
这篇关于如何确定时间范围内的白天时间和夜间时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!