使用python时,写一些时间转换的时候会用到,故整理成博文,以便后续直接拷贝使用!
import time
print(time.time())
# 1622180171.5689292
int(round(time.time() * 1000))
# 毫秒ms
print(time.localtime())
# time.struct_time(tm_year=2021, tm_mon=5, tm_mday=28, tm_hour=13, tm_min=38, tm_sec=30, tm_wday=4, tm_yday=148, tm_isdst=0)
print(time.strftime("%Y-%m-%d_%H-%M-%S", time.localtime()))
# 2021-05-28_13-48-01
time.strftime("%Y-%m-%d", time.localtime(time.mktime(time.localtime()) - 3600 * 24 * num))
print(time.mktime(time.localtime()))
# 1622180522.0
print(time.localtime(1622180171.5689292))
# time.struct_time(tm_year=2021, tm_mon=5, tm_mday=28, tm_hour=13, tm_min=36, tm_sec=11, tm_wday=4, tm_yday=148, tm_isdst=0)
print(time.strftime("%Y-%m-%d_%H-%M-%S", time.localtime(1622180171.5689292)))
# 2021-05-28_13-36-11
print(time.strptime("2021/05/28", '%Y/%m/%d'))
# time.struct_time(tm_year=2021, tm_mon=5, tm_mday=28, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=4, tm_yday=148, tm_isdst=-1)
print(time.mktime(time.strptime("2021/05/28", '%Y/%m/%d')))
# 1622131200.0
import time
if __name__ == '__main__':
startTime = '2021-08-01'
endTime = '2021-08-02'
res = (int)((time.mktime(time.strptime(endTime, '%Y-%m-%d')) - time.mktime(time.strptime(startTime, '%Y-%m-%d'))) / (3600 * 24))
print(res) // 1
# 原时间:2021-05-28_13-36-11
# 字符串时间 --> 时间戳 + 1天 --> 字符串时间
print(time.strftime("%Y-%m-%d_%H-%M-%S", time.localtime(time.mktime(time.strptime("2021-05-28_13-36-11", '%Y-%m-%d_%H-%M-%S')) + 3600 * 24)))
# 一天后:2021-05-29_13-36-11
import time
if __name__ == '__main__':
endTime = time.strftime("%Y-%m-%d", time.localtime())
startTime = time.strftime("%Y-%m-%d", time.localtime(time.mktime(time.strptime(str(endTime), '%Y-%m-%d')) - 3600 * 24 * 30))
print(startTime) # 2021-07-17
print(endTime) # 2021-08-16
from datetime import datetime
print(datetime.now().strftime('%Y-%m-%d_%H-%M-%S-%f'))
# 2021-08-07_09-45-35-589759
持续学习!不断积累!日进一步!