1、时间戳:
import time t = int(time.time()) # 1499825149 秒 t1 = int(round(time.time() * 1000)) # 1666321307232 毫秒 t2 = int(round(time.time() * 1000000)) # 1499825149257892 微秒
2、获取当前时间:
import datetime dt = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') dt_ms = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f') # 含微秒的日期时间,来源 比特量化 print(dt) print(dt_ms) '''结果: 2018-09-06 21:54:46 2018-09-06 21:54:46.205213'''
3、获取UTC时间:
NowDate = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) utcTime = arrow.get(NowDate).to("UTC") UpdateTime = utcTime.strftime("%Y-%m-%dT%H:%M:%SZ")
4、将日期转为时间戳(秒级):
dt = '2018-01-01 10:40:30' ts = int(time.mktime(time.strptime(dt, "%Y-%m-%d %H:%M:%S"))) print (ts) '''结果:1514774430'''
5、将时间戳(秒级)转为日期:
ts = 1515774430 dt = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(ts)) print(dt) '''结果: 2018-01-13 00:27:10'''
6、时间格式转换:
dt = '08/02/2019 01:00' dt_new = datetime.datetime.strptime(dt, '%m/%d/%Y %H:%M').strftime('%Y-%m-%d %H:%M:%S') print(dt_new) '''结果: 2019-08-02 01:00:00'''
7、转结构题时间struct_time
ta_dt = time.strptime("2018-09-06 21:54:46", '%Y-%m-%d %H:%M:%S') #日期时间转结构体 ta_ms = time.localtime(1486188476) #时间戳转结构体,注意时间戳要求为int,来源 比特量化 print(ta_dt) print(ta_ms) '''结果: time.struct_time(tm_year=2018, tm_mon=9, tm_mday=6, tm_hour=21, tm_min=54, tm_sec=46, tm_wday=3, tm_yday=249, tm_isdst=-1) time.struct_time(tm_year=2017, tm_mon=2, tm_mday=4, tm_hour=14, tm_min=7, tm_sec=56, tm_wday=5, tm_yday=35, tm_isdst=0)'''
标签:-%,转换,生成,tm,时间,time,print,dt,datetime From: https://www.cnblogs.com/lala66/p/17276962.html