如何使用 datetime 模块在 Python 中合并两次?
How to combine two times in Python using datetime module?
如何使用 datetime
模块在 Python 中求和两次?
例如 15:33:21.43691
+ 01:12:47.65729
。我应该使用 timedelta
吗?或者还有其他方法吗?谢谢!
时间和日期时间是时间线上的点,所以只能在时间点上加一个timedelta。这些点是相对于原点 datetime.datetime.fromtimestamp(0) >>> datetime.datetime(1970,1,1,1,0)
.
datetime.datetime.today() + datetime.timedelta(weeks=2)
最好的方法是将时钟时间转换为秒,加上它,然后再将其转换回时间。边缘情况将处理从 86400 秒(1 整天)开始的翻转。您可以使用 datetime 中的时间模块来完成此操作。 (从日期时间导入时间)。这样您就可以从 24 小时制添加两次。您仍然需要处理日期翻转的边缘情况,如前所述,即时间超过 24 小时。例如,将 02:00 点添加到 23:00 点。第二天的结果应该是01:00。其实很简单
>>> from datetime import time
>>>
>>> time1 = time(15,33,21,43691)
>>> print(time1)
15:33:21.043691
>>> time1_seconds = time1.hour * 60 * 60 + time1.minute * 60 + time1.second + float( '0.' + str(time1.microsecond))
>>> print(time1_seconds)
56001.43691
>>> time2 = time(1,12,47,65729)
>>> print(time2)
01:12:47.065729
>>> time2_seconds = time2.hour * 60 * 60 + time2.minute * 60 + time2.second + float( '0.' + str(time2.microsecond))
>>> print(time2_seconds)
4367.65729
>>> timeFinal_seconds = time1_seconds + time2_seconds
>>> print(timeFinal_seconds)
60369.0942
>>> timeFinal_seconds = timeFinal_seconds % 86400 #Rolling over incase the day changes,
i.e, the total time becomes more than 24 hours, the clock completes a full cirle. 23:00 + 2 hours will give 01:00 on the clock
>>> print(timeFinal_seconds)
60369.0942
>>> hours
16.76919283333333
>>> minutes = (timeFinal_seconds % 3600) / 60
>>> minutes
46.15156999999999
>>> seconds = (timeFinal_seconds % 3600 % 60)
>>> seconds
9.094199999999546
>>> microseconds = str(seconds).split('.')[-1] # nifty trick to extract the decimal part from a number
>>> microseconds
'094199999999546'
>>> Finaltime = time(int(hours),int(minutes),int(seconds),int(microseconds))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OverflowError: Python int too large to convert to C long
>>> Finaltime = time(int(hours),int(minutes),int(seconds),int(microseconds[0:5])) # taking the first 5 decimal places only using string manipulation to remove the error above
>>> Finaltime
datetime.time(16, 46, 9, 9419)
>>> print(Finaltime)
16:46:09.009419
这是有道理的,将 1 小时 12 分钟添加到下午的 3:33 下午得到 4:46 下午 (这就是你在做你的问题)
如果要显式添加两个日期,可以使用日期时间的时间戳函数class从2个日期中获取日期时间戳,然后添加时间戳,然后将时间戳转换回一个日期。
唯一的问题是时间戳是从 Unix 时间开始计算的,即 1970 年 1 月 1 日 00:00:00 UTC,这并不重要,因为我们都需要一些时间参考点。
因此,如果您添加两个日期 1999 年 12 月 31 日和 2012 年 12 月 31 日,您实际上得到的不是 4000 年,而是 2045 年。
>>> date1 = datetime(1999,12,31)
>>> date2 = datetime(2012,12,31)
>>>
>>> timestamp1 = date1.timestamp()
>>> print(timestamp1)
946578600.0
>>> timestamp2 = date2.timestamp()
>>> print(timestamp2)
1356892200.0
>>>
>>> timestamp_NewDate = timestamp1 + timestamp2
>>>
>>> NewDate = datetime.fromtimestamp(timestamp_NewDate)
>>>
>>> print(NewDate)
2042-12-29 18:30:00
>>>
如何使用 datetime
模块在 Python 中求和两次?
例如 15:33:21.43691
+ 01:12:47.65729
。我应该使用 timedelta
吗?或者还有其他方法吗?谢谢!
时间和日期时间是时间线上的点,所以只能在时间点上加一个timedelta。这些点是相对于原点 datetime.datetime.fromtimestamp(0) >>> datetime.datetime(1970,1,1,1,0)
.
datetime.datetime.today() + datetime.timedelta(weeks=2)
最好的方法是将时钟时间转换为秒,加上它,然后再将其转换回时间。边缘情况将处理从 86400 秒(1 整天)开始的翻转。您可以使用 datetime 中的时间模块来完成此操作。 (从日期时间导入时间)。这样您就可以从 24 小时制添加两次。您仍然需要处理日期翻转的边缘情况,如前所述,即时间超过 24 小时。例如,将 02:00 点添加到 23:00 点。第二天的结果应该是01:00。其实很简单
>>> from datetime import time
>>>
>>> time1 = time(15,33,21,43691)
>>> print(time1)
15:33:21.043691
>>> time1_seconds = time1.hour * 60 * 60 + time1.minute * 60 + time1.second + float( '0.' + str(time1.microsecond))
>>> print(time1_seconds)
56001.43691
>>> time2 = time(1,12,47,65729)
>>> print(time2)
01:12:47.065729
>>> time2_seconds = time2.hour * 60 * 60 + time2.minute * 60 + time2.second + float( '0.' + str(time2.microsecond))
>>> print(time2_seconds)
4367.65729
>>> timeFinal_seconds = time1_seconds + time2_seconds
>>> print(timeFinal_seconds)
60369.0942
>>> timeFinal_seconds = timeFinal_seconds % 86400 #Rolling over incase the day changes,
i.e, the total time becomes more than 24 hours, the clock completes a full cirle. 23:00 + 2 hours will give 01:00 on the clock
>>> print(timeFinal_seconds)
60369.0942
>>> hours
16.76919283333333
>>> minutes = (timeFinal_seconds % 3600) / 60
>>> minutes
46.15156999999999
>>> seconds = (timeFinal_seconds % 3600 % 60)
>>> seconds
9.094199999999546
>>> microseconds = str(seconds).split('.')[-1] # nifty trick to extract the decimal part from a number
>>> microseconds
'094199999999546'
>>> Finaltime = time(int(hours),int(minutes),int(seconds),int(microseconds))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OverflowError: Python int too large to convert to C long
>>> Finaltime = time(int(hours),int(minutes),int(seconds),int(microseconds[0:5])) # taking the first 5 decimal places only using string manipulation to remove the error above
>>> Finaltime
datetime.time(16, 46, 9, 9419)
>>> print(Finaltime)
16:46:09.009419
这是有道理的,将 1 小时 12 分钟添加到下午的 3:33 下午得到 4:46 下午 (这就是你在做你的问题)
如果要显式添加两个日期,可以使用日期时间的时间戳函数class从2个日期中获取日期时间戳,然后添加时间戳,然后将时间戳转换回一个日期。
唯一的问题是时间戳是从 Unix 时间开始计算的,即 1970 年 1 月 1 日 00:00:00 UTC,这并不重要,因为我们都需要一些时间参考点。
因此,如果您添加两个日期 1999 年 12 月 31 日和 2012 年 12 月 31 日,您实际上得到的不是 4000 年,而是 2045 年。
>>> date1 = datetime(1999,12,31)
>>> date2 = datetime(2012,12,31)
>>>
>>> timestamp1 = date1.timestamp()
>>> print(timestamp1)
946578600.0
>>> timestamp2 = date2.timestamp()
>>> print(timestamp2)
1356892200.0
>>>
>>> timestamp_NewDate = timestamp1 + timestamp2
>>>
>>> NewDate = datetime.fromtimestamp(timestamp_NewDate)
>>>
>>> print(NewDate)
2042-12-29 18:30:00
>>>