1、获取当前时间
from datetime import datetime datetime_object = datetime.now() 2021-12-15 14:18:48.711364 <'datetime'> d1 = datetime.now().replace(microsecond=0) 2021-12-15 14:20:44 <'datetime'> d1 = datetime.now().strftime("%Y-%m-%d %H:%M:%S") 2021-12-15 14:30:09 <'str'>
2、获取时间戳
d2 = datetime.timestamp(datetime.now()) 1639554245.122323 < 'float'> d2 = int(time.time()) 1639554245 < 'int'>
3、时间格式转换
str --> datetime date_str = "2021-11-30 13:53:59" d4 = datetime.strptime(date_str, "%Y-%m-%d %H:%M:%S") <'datetime'>
datetime --> str dt_obj = datetime(2020, 11, 30, 13, 53, 59) d5 = dt_obj.strftime("%Y-%m-%d %H:%M:%S") <'str'>
timestamp --> datetime timestamp = 1480486369 d6 = datetime.fromtimestamp(timestamp) <'datetime'>
datetime --> timestamp d7 = time.mktime(d6.timetuple()) < 'float'>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)