我是靠谱客的博主 魁梧小甜瓜,这篇文章主要介绍python中datetime默认的1990年改为_Python:将’1990年以来的天数’转换为datetime对象...,现在分享给大家,希望可以做个参考。

日期时间模块的

timedelta可能正是您所需要的.

例如:

from datetime import date, timedelta

days = 9465 # This may work for floats in general, but using integers

# is more precise (e.g. days = int(9465.0))

start = date(1990,1,1) # This is the "days since" part

delta = timedelta(days) # Create a time delta object from the number of days

offset = start + delta # Add the specified number of days to 1990

print(offset) # >>> 2015-12-01

print(type(offset)) # >>>

然后,您可以使用和/或操纵偏移对象,或将其转换为字符串表示,但您认为合适.

您可以像使用time_datetime一样使用与此日期对象相同的格式:

print(offset.strftime('%Y-%m-%d %H:%M:%S'))

输出:

2015-12-01 00:00:00

例如,如果您稍后要向其添加小时/分钟/秒/时区偏移,则可以使用日期时间对象,而不是使用日期对象.

代码将保持与上面相同,但两行除外:

# Here, you're importing datetime instead of date

from datetime import datetime, timedelta

# Here, you're creating a datetime object instead of a date object

start = datetime(1990,1,1) # This is the "days since" part

注意:虽然您没有说明,但另一个答案表明您可能正在寻找时区感知日期时间.如果是这种情况,正如另一个答案所暗示的那样,dateutil是Python 2的发展方式.在Python 3中,您需要使用datetime模块的tzinfo.

最后

以上就是魁梧小甜瓜最近收集整理的关于python中datetime默认的1990年改为_Python:将’1990年以来的天数’转换为datetime对象...的全部内容,更多相关python中datetime默认内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(139)

评论列表共有 0 条评论

立即
投稿
返回
顶部