%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 当前时区的名称 %% %号本身
# 重点:时间戳 -> 可以作为数据的唯一标识 是相对于1970-1-1-0:0:0 时间插值 ------------------time.time()-------------------- import time print(time) #<module 'time' (built-in)> print(time.time()) #1554879042.493756(至2019.4.10的秒数,精确到秒后6位小数) #将时间戳转化为时区 ------------------time.localtime()-------------------- print(time.localtime(1554879042).tm_year) #2019 print(time.localtime(1554879042)) print(time.localtime(time.time())) #time.struct_time(tm_year=2019, tm_mon=4, tm_mday=10, tm_hour=15, tm_min=4, tm_sec=29, tm_wday=2, tm_yday=100, tm_isdst=0) pirnt(time.localtime) #当前时区时间:东八区(上海时区)time.struct_time(tm_year=2019, tm_mon=4, tm_mday=10, tm_hour=14, tm_min=55, tm_sec=7, tm_wday=2, tm_yday=100, tm_isdst=0) print(time.localtime()[0]) #2019 print(time.localtime().tm_year) #2019 print(time.gmtime()) #格林威治时区 # 格式化时间 # 格式化的字符串,时间tuple ------------------time.strftime()-------------------- res = time.strftime('%Y-%m-%d %j days') print(res) #2019-04-10 100 days 只支持ASCII,即英文、特殊符号 t = (2020, 4, 10, 10, 19, 22, 2, 200, 0) res = time.strftime('%Y-%m-%d %j days', t) #2020-04-10 200 days print(res) # 没有确保数据的安全性,只是将元组信息转化为时间格式的字符串
# 需求:输入一个年份,判断其是否是闰年 # 1.能被400整除 year % 400 == 0 # 2.能被4整除不能被100整除 year % 4 == 0 and year % 100 != 0 year = int(input('year: ')) b1 = year % 400 == 0 b2 = year % 4 == 0 and year % 100 != 0 if b1 or b2: print("是闰年") else: print("不是闰年") -------------------------------------------------- import calendar print(calendar.isleap(2018)) >>>False
import calendar print(calendar.month(2019,4)) print(calendar.monthrange(2019,4)) print(calendar.weekday(2019,4,10)) -------------------------------------------- April 2019 Mo Tu We Th Fr Sa Su 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 (0, 30) 2 #周一:0
tm1=datetime.datetime.now()+ datetime.timedelta(days=-1) print(tm1) #datetime <class 'datetime.datetime'> 2019-04-10 16:07:31.071055 day = datetime.timedelta(days=1) print(day, type(day)) # 1 day, 0:00:00 <class 'datetime.timedelta'> # tm与day都是对象,可以直接做运算 print(tm - day) #2019-04-09 16:03:57.318889 # tm是对象,还可以接着调用方法 print(tm.replace(year=2200)) #2200-04-09 16:03:57.318889 print(datetime.date.fromtimestamp(5656565653)) #2149-04-01 ----------------------------------------------------------
import time tm_t=time.time() print('time',type(tm_t),tm_t) import calendar tm_c=calendar.calendar(2019) print('calendar',type(tm_c)) import datetime tm=datetime.datetime.now() print('datetime',type(tm),tm) ----------------------------------------------- time.time() <class 'float'> 1554882569.767177 calendar.calendar(2019) <class 'str'> datetime.datetime.now() <class 'datetime.datetime'> 2019-04-10 15:49:29.781145