时间模块
time和datetime
时间表示分类:
时间戳:time.time()
格式化的时间字符串
元组:time.localtime()
time方法:
time(),查看时间戳
sleep(n),线程休眠n秒
gmtime(),时间戳-->元组时间,utc时间
localtime(),时间戳-->元组时间,转换到本地时区的形式,
mktime(),元组时间-->时间戳
strftime(),元组时间-->格式化字符串
time.strptime,格式化字符串-->元组时间
asctime():元组时间到指定格式字符串,如Tue Feb 27 11:59:16 2018
ctime():时间戳到字符串
datetime
MAXYEAR:9999
MINYEAR:0001
date
a = datetime.date(2018,3,1) b = datetime.date(2018,3,2) c = datetime.date.today() d = datetime.date(2018,2,22) # datetime.date类,3个参数,年月日 c = datetime.date(2018, 2, 25)
c.year=2018
c.month=2
c.day=25 # 判断一个date类型的变量是星期几,sunday==7 print(datetime.date.isoweekday(c)) # 判断一个date类型的变量是星期几,sunday==6 print(datetime.date.weekday(c)) # 输出当前年月日 print(datetime.date.today()) # 把一个date类型的数据输出成格式化的字符串格式时间 print(datetime.date.ctime(c)) # 输入一个整数n,输出这个整数是公元日期,参数n代表日 print(datetime.date.fromordinal(736770)) # 输入一个整数n,输出1970-01-01开始的n秒的日期 print(datetime.date.fromtimestamp(213213)) # 转化类型: 日期-->元组 print(datetime.date.isocalendar(c)) # 转化类型: 日期-->字符串 print(datetime.date.isoformat(c)) print(datetime.date.__str__(c)) print(c.__str__()) # 不知道,返回解决序列? print(datetime.date.mro()) # 日期替代 print(datetime.date.replace(c, year=2222, month=3, day=11)) # 格式化日志 print(datetime.date.strftime(c, "%m,%d,%Y")) # 转化类型: 日期-->元组时间 print(datetime.date.timetuple(c)) # 输入时间类型:返回整数,公元多少天 print(datetime.date.toordinal(c)) # a等于b,返回true,否则false print(datetime.date.__eq__(a, b)) print(a.__eq__(b)) # a不等于c,返回true,否则false print(datetime.date.__ne__(a, c)) print(a.__ne__(c)) # b大于等于a,返回true,否则false print(datetime.date.__ge__(b, a)) print(b.__ge__(a)) # a大于d,返回true,否则false print(datetime.date.__gt__(a, d)) print(a.__gt__(d)) # d小于等于a,返回true,否则false print(datetime.date.__le__(d, a)) # a小于b,返回true,否则false print(datetime.date.__lt__(a, b)) # 查询日期间隔,从a到b print(datetime.date.__sub__(a, b)) print(a.__sub__(b)) # 查询日期间隔,从b到a print(datetime.date.__rsub__(a, b)) print(a.__rsub__(b)) # 最大和最小日期 print(datetime.date.max) print(datetime.date.min)
time
a = datetime.time(23,59,59,212) b = datetime.time(7,2,3,456) print(a) print(a.hour) print(a.minute) print(a.second) print(a.microsecond) print(a.tzinfo) print(datetime.time.strftime(a,'%H……%M……%S')) # a大于b,返回true,否则false print(a.__gt__(b)) # a大于等于b,返回true,否则false print(a.__ge__(b)) # a小于b,返回true,否则false print(a.__lt__(b)) # a小于等于b,返回true,否则false print(a.__le__(b)) # a等于b,返回true,否则false print(a.__eq__(b)) # a不等于b,返回true,否则false print(a.__ne__(b)) print(datetime.time.max) print(datetime.time.min) # 不知道 print(datetime.time.resolution) # 格式化输出 print(a.__format__("%S~%M~%H")) print(a.strftime("%S~%M~%H")) # 标准格式化 print(a.isoformat()) # 转换到字符串 print(a.__str__()) # 时间替换 print(a.replace(hour=4,minute=44,second=44,microsecond=444444))
datetime
a = datetime.datetime.now() b = datetime.date(2020,2,20) c = datetime.time(23,58,59,666666) print(a) print(a.date()) print(a.time()) # 元组时间 print(a.utctimetuple()) # 把date类型的变量和一个time类型的变量,组成一个datetime print(datetime.datetime.combine(b,c)) # 当前时间 print(datetime.datetime.now()) # 当前utc时间 print(datetime.datetime.utcnow()) # 把字符串返回为datetime对象 print(datetime.datetime.strptime('2017-01-01 22:22', '%Y-%m-%d %H:%M')) # 时间戳格式-->datetime对象 print(datetime.datetime.utcfromtimestamp(time.time()))
timedelta
默认情况下的证书是day,比如datetime.timedelta(1)表示一天,有3个属性,如下
t1 = datetime.datetime.now() dt = datetime.datetime.combine(b,c) - t1 print(dt) print(dt.days) print(dt.seconds) print(dt.microseconds)
格式化时间的格式符号
%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) %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 当前时区的名称 %% %号本身
tzinfo