首页 > 代码库 > Python 时间处理---------笔记

Python 时间处理---------笔记

时区处理&格式化

import pytz
from datetime import datetime
# 设置时区
timezone = pytz.timezone(‘Asia/Shanghai‘)

# 格式化
now_time = datetime.now(timezone).strftime("%Y-%m-%d %H:%M:%S")
print(now_time)

 

直接输出当前时间戳

import time

now_timestamps = time.time()
print(now_timestamps)

datetime time 转换

import datetime
import time
import pytz

str = 2016-10-26 23:12:19

str2datetime = datetime.datetime.strptime(str, %Y-%m-%d %H:%M:%S)
# print(str2datetime)

datetime2str = str2datetime.strftime(%Y-%d-%m %H:%M:%S)
# print(datetime2str)

time_tuple = str2datetime.timetuple()
# print(time_tuple)

time2str = time.strftime("%Y-%m-%d %H:%M:%S", time_tuple)
# print(time2str)

tuple2datetime = datetime.datetime(*time_tuple[0:6])
# print(tuple2datetime)

now_timestamps = time.time()
# print(now_timestamps)
time2datetime = datetime.datetime.fromtimestamp(now_timestamps)

time_tuple = time.localtime(now_timestamps)
print(time_tuple)

#本地时区时间
local = datetime.datetime.fromtimestamp(now_timestamps)
print(local)
#标准时区时间
standard = datetime.datetime.utcfromtimestamp(now_timestamps)
print(standard)

#本地时区时间
local_tz = time.localtime(now_timestamps)
print(local_tz)
#标准时区时间
standard_tz = time.gmtime(now_timestamps)
print(standard_tz)

 

Python 时间处理---------笔记