datetime包
timedelta(params…)得到一个时间增量对象
from datetime import timedelta
if __name__ == '__main__':
delta = timedelta(hours=2)
print(delta)
print(type(delta))
timezone(timedelta) + timedelta(params…) 创建时区对象
from datetime import timedelta, timezone
if __name__ == '__main__':
delta = timedelta(hours=2)
zone = timezone(delta)
print(zone)
print(type(zone))
datetime模块
datetime.now(timezone) 获取当前时间datetime对象
from datetime import timedelta, timezone, datetime
if __name__ == '__main__':
'''
获取当前时间,可以获取指定时区的当前时间
datetime.now(timezone)
'''
now = datetime.now()
print(now)
print(type(now))
print(datetime.now((timezone(timedelta(hours=9)))))
datetime.strftime(fmt) datetime时间对象转字符串
from datetime import datetime
if __name__ == '__main__':
'''
datetime.strftime(fmt)
将时间对象转换成字符串
fmt:格式化标准,由格式符组成
常用格式符(年:%Y,月:%m,日:%D,时:%H,分:%M,秒:%S)
'''
now = datetime.now()
print(now.strftime('%Y-%m-%d %H:%M:%S'))
datetime.strptime(date_string,fmt) 字符串转成datetime时间对象
from datetime import datetime
if __name__ == '__main__':
'''
datetime.strptime(date_string,fmt)
将字符串转换成时间对象,要求date_string的格式完全匹配fmt格式化标准
'''
time_obj = datetime.strptime('2022-2-22', '%Y-%m-%d')
print(datetime.strptime('2022-2-22 14', '%Y-%m-%d %H'))
print(time_obj)
print(type(time_obj))
datetime.timestamp(datetime_obj) 将datetime时间对象转换成秒级时间戳
from datetime import datetime
if __name__ == '__main__':
'''
datetime.timestamp(datetime_obj)
datetime_obj:datetime 时间对象
返回 float
'''
print(datetime.timestamp(datetime.now()))
datetime.fromtimestamp(t) 将秒级时间戳转换成datetime时间对象
from datetime import datetime, timedelta, timezone
if __name__ == '__main__':
'''
datetime.fromtimestamp(t)
t:秒级时间戳 float类型
返回:datetime时间对象
'''
datetime_obj = datetime.fromtimestamp(1645598565.715)
print(datetime_obj)
print(type(datetime_obj))
使用datetime对象 + timedelta(params…) 进行时间运算
from datetime import datetime, timedelta, timezone
if __name__ == '__main__':
now = datetime.now()
fmt = '%Y-%m-%d %H:%M:%S'
print(now.strftime(fmt))
print((now + timedelta(hours=3)).strftime(fmt))
print((now - timedelta(hours=3)).strftime(fmt))
print((now + timedelta(hours=-3)).strftime(fmt))
time包
time.time() 得到当前秒级时间戳
import time
if __name__ == '__main__':
print(time.time())
time.localtime(second) 将秒转换成time时间对象
import time
if __name__ == '__main__':
t = time.localtime(time.time())
t2 = time.localtime()
print(t)
print(t2)
print(type(t))
print(type(t2))
time.strftime(fmt,time_obj) 将time时间对象转换成字符串
import time
if __name__ == '__main__':
"""
time.strftime(fmt,time_obj)
fmt:格式化标准 参考 datetime.strftime(fmt)
time_obj:time时间对象,不填默认是当前日期的time时间对象
"""
t = time.localtime(time.time() + 3600)
print(time.strftime('%Y-%m-%d %H:%M:%S'))
print(time.strftime('%Y-%m-%d %H:%M:%S', t))
time.strptime(time_string,fmt) 将字符串转换成time时间对象
import time
if __name__ == '__main__':
"""
time.strptime(time_string,fmt)
参考 datetime.strptime(date_string,fmt)
time_string:时间字符串
fmt:格式化标准
"""
fmt = '%Y-%m-%d %H:%M:%S'
t = time.strftime(fmt, time.localtime())
print(t)
print(time.strptime(t, fmt))
time.sleep(second) 休眠 second 秒
import time
if __name__ == '__main__':
print(time.time())
time.sleep(2)
print(time.time())
|