time和datetime
1.常用的时间格式符号
# 本次用到的格式符号
%y 两位数的年份表示(00-99)
%Y 四位数的年份表示(000-9999)
%m 月份(01-12)
%d 月内中的一天(0-31)
%H 24小时制小时数(0-23)
%I 12小时制小时数(01-12)
%M 分钟数(00-59)
%S 秒(00-59)
2.time
#1.获取当前的时间戳 time.time()返回的一个float型,是从1970年1月1日0时起到当前经过的秒数,注意这里是分时区的
In [37]: time.time()
Out[37]: 1576943894.6760898
#2.时间戳转化为字符串
In [38]: time.strftime("%Y-%m-%d %H:%M:%S")
Out[38]: '2019-12-21 23:58:30'
# 时间字符串转为time格式
In [39]: time.strptime('2019-12-21 23:25:24',"%Y-%m-%d %H:%M:%S")
Out[39]: time.struct_time(tm_year=2019, tm_mon=12, tm_mday=21, tm_hour=23, tm_min=25, tm_sec=24, tm_wday=5, tm_yday=355, tm_isdst=-1)
#时间字符串转化为float
In [40]: time.mktime(time.strptime('2019-12-21 23:25:24',"%Y-%m-%d %H:%M:%S"))
Out[40]: 1576941924.0
#float类型转成时间字符串
In [41]: time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(1576941924.0))
Out[41]: '2019-12-21 23:25:24'
#time tuple格式转成字符串
In [47]: time_tuple = (2019, 12, 21, 23, 25, 24, 2, 317, 0)
In [48]: time.strftime("%Y-%m-%d %H:%M:%S", time_tuple)
Out[48]: '2019-12-21 23:25:24'
#3.获取当前时间 英文字符串
In [49]: time.ctime()
Out[49]: 'Sun Dec 22 00:03:15 2019'
#4.当前时间的time结构体 time.localtime()返回的是一个time结构体,其中包括tm_year,tm_mon,tm_mday,tm_hour,tm_min,tm_sec,tm_wday,tm_yday,tm_isdst=0(夏令时间标志)
In [51]: time.localtime()
Out[51]: time.struct_time(tm_year=2019, tm_mon=12, tm_mday=22, tm_hour=0, tm_min=3, tm_sec=42, tm_wday=6, tm_yday=356, tm_isdst=0)
#5.显示utc时间 和北京时间差8个小时
In [52]: time.gmtime()
Out[52]: time.struct_time(tm_year=2019, tm_mon=12, tm_mday=21, tm_hour=16, tm_min=3, tm_sec=56, tm_wday=5, tm_yday=355, tm_isdst=0)
3.datetime
#1.获取当前时间
In [35]: datetime.datetime.now()
Out[35]: datetime.datetime(2019, 12, 21, 23, 57, 37, 731054)
#2.datetime转为float类型
In [34]: time.mktime(datetime.datetime.now().timetuple())
Out[34]: 1576943839.0
# datetime 转化为字符串
In [57]: datetime.datetime.now().strftime("%Y-%m-%d %H:%S:%M")
Out[57]: '2019-12-22 00:31:08'
# 字符串转化为datetime
In [58]: datetime.datetime.strptime('2019-12-22 00:31:08',"%Y-%m-%d %H:%S:%M")
Out[58]: datetime.datetime(2019, 12, 22, 0, 8, 31)
#3.修改当前时间的秒数和毫秒数
In [32]: datetime.datetime.now().replace(second=59, microsecond=99)
Out[32]: datetime.datetime(2019, 12, 21, 23, 56, 59, 99)
In [33]: datetime.datetime.now().replace(day=22,second=59, microsecond=99)
Out[33]: datetime.datetime(2019, 12, 22, 23, 56, 59, 99)
# datetime 转为时间戳
In [31]: datetime.datetime.now().replace(day=22,second=59, microsecond=99).timestamp()
Out[31]: 1577030159.000099
#4.比较time和datetime的时间戳差别
In [29]: datetime.datetime.now().timestamp()
Out[29]: 1576943655.772876
In [30]: time.time()
Out[30]: 1576943657.0763087
#5.当前的天
In [54]: datetime.date.today()
Out[54]: datetime.date(2019, 12, 22)
In [77]: datetime.datetime.now().day
Out[77]: 22
In [78]: datetime.datetime.now().hour
Out[78]: 0
In [79]: datetime.datetime.now().second
Out[79]: 57
In [80]: datetime.datetime.now().minute
Out[80]: 24
#6.datetime date和time转换
In [64]: a = datetime.date(2019, 12, 22)
In [65]: datetime.datetime.strptime(str(a),'%Y-%m-%d')
Out[65]: datetime.datetime(2019, 12, 22, 0, 0)
# 获取两个时间相差的秒数
In [66]: (datetime.datetime.now() - datetime.datetime(1970, 1,1,0,0, 0,0)).total_seconds()
Out[66]: 1576973786.296571
In [70]: (datetime.datetime.now()-datetime.datetime(2019,12,22,0, 0, 0,0)).total_seconds()
Out[70]: 1108.34737
#7.前一个小时 前一天
In [75]: (datetime.datetime.now()-datetime.timedelta(hours=1)).strftime("%Y-%m-%d %H:%S:%M")
Out[75]: '2019-12-21 23:37:20'
In [76]: (datetime.datetime.now()-datetime.timedelta(days=1)).strftime("%Y-%m-%d %H:%S:%M")
Out[76]: '2019-12-21 00:33:21'
# 获取当前时间
now = datetime.datetime.now()
# 获取今天零点
In [86]: now = datetime.datetime.now()
In [87]: now - datetime.timedelta(hours=now.hour, minutes=now.minute, seconds=now.second, microseconds=now.microsecond)
Out[87]: datetime.datetime(2019, 12, 22, 0, 0)
# 获取23:59:59
In [90]: datetime.datetime(2019, 12, 22, 0, 0) + datetime.timedelta(hours=23, minutes=59, seconds=59)
Out[90]: datetime.datetime(2019, 12, 22, 23, 59, 59)
In [92]: datetime.datetime(2019, 12, 22, 0, 0).replace(hour=23,minute=59,second=59)
Out[92]: datetime.datetime(2019, 12, 22, 23, 59, 59)
# 获取前一天的当前时间
In [88]: now - datetime.timedelta(hours=23, minutes=59, seconds=59)
Out[88]: datetime.datetime(2019, 12, 21, 0, 29, 41, 815880)
# 获取明天的当前时间
In [89]: now + datetime.timedelta(hours=23, minutes=59, seconds=59)
Out[89]: datetime.datetime(2019, 12, 23, 0, 29, 39, 815880)
4.恩怨纠葛
# 将float转化为datetime 间接和time模块建立链接
In [96]: datetime.datetime.fromtimestamp(time.time())
Out[96]: datetime.datetime(2019, 12, 22, 0, 45, 12, 389693)
time相关的数据类型 float ,struct_time ,string
time.mktime(struct_time) struct_time=>float
time.strftime(format,struct_time) struct_time=>string
time.strptime(string,struct_time) string=>struct_time
time.localtime(float) float=>struct_time
datetime相关的数据类型 datetime,string ,struct_time,float
datetime.datetime.strftime(format) datetime=>string
datetime.datetime.strptime(string,format) string=>datetime
datetime.datetime.fromtimestamp(float) float=>datetime
5.不常用的格式符号
%a 本地简化星期名称
%A 本地完整星期名称
%b 本地简化的月份名称
%B 本地完整的月份名称
%c 本地相应的日期表示和时间表示
%j 年内的一天(001-366)
%p 本地A.M.或P.M.的等价符
%U 一年中的星期数(00-53)星期天为星期的开始
%w 星期(0-6),星期天为星期的开始
%W 一年中的星期数(00-53)星期一为星期的开始
%x 本地相应的日期表示
%X 本地相应的时间表示
%Z 当前时区的名称
%% %号本身