引用:https://blog.csdn.net/p9bl5bxp/article/details/54945920
菜鸟教程-Python 日期和时间
>>> time.time()
1586418585.5257692
>>> time.localtime()
time.struct_time(tm_year=2020, tm_mon=4, tm_mday=9, tm_hour=15, tm_min=53, tm_sec=39, tm_wday=3, tm_yday=100, tm_isdst=0)
>>> time.ctime()
'Thu Apr 9 15:54:48 2020'
>>> time.asctime()
'Thu Apr 9 15:54:59 2020'
>>> time.localtime(1586418585.5257692)
time.struct_time(tm_year=2020, tm_mon=4, tm_mday=9, tm_hour=15, tm_min=49, tm_sec=45, tm_wday=3, tm_yday=100, tm_isdst=0)
>>> st=time.localtime(1586418585.5257692)
>>> time.mktime(st)
1586418585.0
>>> time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
'2020-04-09 16:00:17'
>>> time.strftime('%x %X', time.localtime())
'04/09/20 16:00:39'
>>> s='2020-04-09 16:00:17'
>>> time.strptime(s,'%Y-%m-%d %H:%M:%S')
time.struct_time(tm_year=2020, tm_mon=4, tm_mday=9, tm_hour=16, tm_min=0, tm_sec=17, tm_wday=3, tm_yday=100, tm_isdst=-1)
类之间的关系:
object
date
datetime
time
timedelta
tzinfo
timezone
class datetime.date(year, month, day)
# 返回一个表示当前本地日期的date对象
>>> datetime.date.today()
datetime.date(2020, 4, 9)
>>> datetime.date.today().year
2020
>>> datetime.date.today().month
4
>>> datetime.date.today().day
9
# 返回‘YYYY-MM-DD’格式的日期字符串
>>> datetime.date.today().isoformat()
'2020-04-09'
# 返回指定格式的日期字符串,与time模块的strftime(format, struct_time)功能相同
>>> datetime.date.today().strftime('%x')
'04/09/20'
# 返回日期对应的time.struct_time对象
>>> datetime.date.today().timetuple()
time.struct_time(tm_year=2020, tm_mon=4, tm_mday=9, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=100, tm_isdst=-1)
# 根据跟定的时间戳,返回一个date对象
>>> datetime.date.fromtimestamp(1586419123.0)
datetime.date(2020, 4, 9)
class datetime.time(hour, [minute[, second, [microsecond[, tzinfo]]]])
# 构造time对象
>>> t = time(20, 5, 40, 8888)
>>> t.hour
20
>>> t.minute
5
>>> t.second
40
>>> t.microsecond
8888
>>> t.isoformat()
'20:05:40.008888'
>>> t.strftime('%H%M%S')
'200540'
>>> t.strftime('%H%M%S.%f')
'200540.008888'
class datetime.datetime(year, month, day, hour=0, minute=0, second=0, microsecond=0, tzinfo=None)
# 返回一个表示当前本期日期时间的datetime对象
>>> datetime.datetime.now() # 可指定时区
datetime.datetime(2020, 4, 9, 16, 20, 6, 409636)
>>> datetime.datetime.today()
datetime.datetime(2020, 4, 9, 16, 20, 12, 703556)
# 根据指定的时间戳创建一个datetime对象
>>> datetime.datetime.fromtimestamp(1586419123.0)
datetime.datetime(2020, 4, 9, 15, 58, 43)
# 将时间字符串转换为datetime对象
>>> datetime.datetime.strptime('2020-04-09 16:00:17', '%Y-%m-%d %H:%M:%S')
datetime.datetime(2020, 4, 9, 16, 0, 17)
# 获取datetime对象对应的date对象
>>> datetime.datetime.today().date()
datetime.date(2020, 4, 9)
# 获取datetime对象对应的time对象
>>> datetime.datetime.today().time()
datetime.time(16, 23, 28, 9313)
# 返回datetime对应的time.struct_time对象
>>> datetime.datetime.today().timetuple()
time.struct_time(tm_year=2020, tm_mon=4, tm_mday=9, tm_hour=16, tm_min=24, tm_sec=39, tm_wday=3, tm_yday=100, tm_isdst=-1)
# 返回固定格式的时间字符串
>>> datetime.datetime.today().isoformat()
'2020-04-09T16:25:35.846289'
# 返回指定格式的时间字符串
>>> datetime.datetime.today().strftime('%Y-%m-%d %H:%M:%S')
'2020-04-09 16:28:11'
datetime.datetime类对时间戳与时间字符串进行转换
class datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, hours=0, weeks=0)
# 返回一个时间间隔对象,可以直接与datetime.datetime对象做加减操作
>>> datetime.timedelta(365)
datetime.timedelta(days=365)
>>> datetime.timedelta(365).total_seconds() # 一年包含的总秒数
31536000.0
>>> dt = datetime.datetime.now()
>>> dt + datetime.timedelta(3) # 3天后
datetime.datetime(2017, 2, 8, 9, 39, 40, 102821)
>>> dt + datetime.timedelta(-3) # 3天前
datetime.datetime(2017, 2, 2, 9, 39, 40, 102821)
>>> dt + datetime.timedelta(hours=3) # 3小时后
datetime.datetime(2017, 2, 5, 12, 39, 40, 102821)
>>> dt + datetime.timedelta(hours=-3) # 3小时前
datetime.datetime(2017, 2, 5, 6, 39, 40, 102821)
>>> dt + datetime.timedelta(hours=3, seconds=30) # 3小时30秒后
datetime.datetime(2017, 2, 5, 12, 40, 10, 102821)