标签:格式化 struct 支持 转换 大小写 mtime time() import mkt
import time
支持三种不同形式的时间,不同形式的时间之间可以转换
print(time.time())
--------------------------------------------------------
# 1565922341.6068554
print(time.strftime('%Y_%m_%d %X')) #大小写一定要写对
--------------------------------------------------------------
2019_08_16 19:20:23
print(time.localtime())print(time.gmtime())
--------------------------------------------------------------
time.struct_time(tm_year=2019, tm_mon=8, tm_mday=16, tm_hour=19, tm_min=21, tm_sec=28, tm_wday=4, tm_yday=228, tm_isdst=0) #北京时间
time.struct_time(tm_year=2019, tm_mon=8, tm_mday=16, tm_hour=11, tm_min=21, tm_sec=28, tm_wday=4, tm_yday=228, tm_isdst=0) #世界标准时间
print(time.strftime('%Y_%M_%d %x',time.localtime()))
--------------------------------------------------------------
2019_08_16 19:24:08
print(time.strptime("2019_08_16 14:39:21",'%Y_%m_%d %X'))
--------------------------------------------------------------
time.struct_time(tm_year=2019, tm_mon=8, tm_mday=16, tm_hour=14, tm_min=39, tm_sec=21, tm_wday=4, tm_yday=228, tm_isdst=-1)
print(time.mktime(time.localtime()))
--------------------------------------------------------------
1565954752.0
print(time.localtime((time.time())))
--------------------------------------------------------------
time.struct_time(tm_year=2019, tm_mon=8, tm_mday=16, tm_hour=19, tm_min=26, tm_sec=24, tm_wday=4, tm_yday=228, tm_isdst=0)
import datetime
做时间的修改
print(datetime.datetime.now()) #当前时间
----------------------------------------------------------
2019-08-16 19:31:24.628613
print(now+datetime.timedelta(3)) #加三天
--------------------------------------------------------------
2019-08-19 19:31:24.630608
print(now+datetime.timedelta(hours=3)) #加三小时
-------------------------------------------------------------
2019-08-16 22:31:24.630608
print(now+datetime.timedelta(minutes=3)) #加三分钟
-----------------------------------------------------------
2019-08-16 19:34:24.630608
print(now.replace(year=1900)) #可以做时间的更改
------------------------------------------------------------
1900-08-16 19:31:24.630608
标签:格式化 struct 支持 转换 大小写 mtime time() import mkt
原文地址:https://www.cnblogs.com/yanjiayi098-001/p/11365972.html