1.time的使用
import time
print(time.time())#打印当前时间戳
print(time.localtime())#打印当前时间元组
print(time.asctime(time.localtime()))#打印内置时间格式
print(time.strftime('%Y-%m-%d',time.localtime()))#将时间元组转变为字符串
print(time.strptime('2018-06-15','%Y-%m-%d'))#将字符串转转换为时间元组
print(time.mktime(time.strptime('2018-06-15','%Y-%m-%d')))#将时间元组转变为时间戳
>>:
1529034110.4935222
time.struct_time(tm_year=2018, tm_mon=6, tm_mday=15, tm_hour=11, tm_min=41, tm_sec=50, tm_wday=4, tm_yday=166, tm_isdst=0)
Fri Jun 15 11:41:50 2018
2018-06-15
time.struct_time(tm_year=2018, tm_mon=6, tm_mday=15, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=4, tm_yday=166, tm_isdst=-1)
1528992000.0
2.datetimede的使用
import datetime
print(datetime.datetime.now())#打印当前时间
print(datetime.datetime.strftime(datetime.datetime.now(),'%Y-%m-%d'))#打印日期格式化字符串
#日期运算
print(datetime.datetime.now()+datetime.timedelta(days=2))
>>:
2018-06-15 11:41:50.495522
2018-06-15
2018-06-17 11:41:50.495522
转载于:https://blog.51cto.com/13803166/2129738