Python:时间处理,time和datetime

# time
import time
v1 = time.time()
print(v1)  # 1635057205.2733288,1970年起到现在过了多少秒

v2 = time.timezone
print(v2)  # 东八区秒数,28800/3600 = 8

print('开始')
v3 = time.sleep(0.5)  # 等三秒
print('结束')

# datatime
from datetime import datetime,timezone,timedelta

v4 = datetime.now()
print(v4)  # 2021-10-24 14:38:22.484424当前东八区时间

v5 = datetime.utcnow()
print(v5)  # 当前UTC时间:2021-10-24 06:39:14.267374

v6 = timezone(timedelta(hours=7))
v7 = datetime.now(v6)
print(v7)  # 2021-10-24 13:40:34.090054+07:00

# 时间加减
v8 = v4 + timedelta(days=100,minutes=30)
print(v8)  # 2022-02-01 14:54:06.730231,加上100天、30分钟

v9 = v8 - v4  # 时间相减,己算间隔时间(不能相加)
print(v9.days,v9.seconds,v9.microseconds)  # 100 1800 0

# datetime类型可以相减或者datetime类型之间可以比较,但不能相加

# 字符串格式转换成datetime格式
text = '2020-12-24'
v10 = datetime.strptime(text,'%Y-%m-%d')
print(v10)  # 2020-12-24 00:00:00

# datetime格式转换成字符串格式
v11 = datetime.strftime(v4,'%Y-%m-%d %H:%M:%S')
print(v11)  # 2021-10-24 17:12:36

# 时间戳格式转换成datetime格式
v12 = time.time()
print(v12)  # 1635066941.1208205
v13 = datetime.fromtimestamp(v12)
print(v13)  # 2021-10-24 17:15:41.120821

# datetime转换成时间戳
v14 = v4.timestamp()
print(v14)  # 1635067030.341215
上一篇:KPM AQUA-PICCOLO


下一篇:在Ubuntu16.04上安装Docker