Python datetime模块的一些简单使用

from datetime import datetime
import pytz

tz = pytz.timezone('Asia/Shanghai')

now = lambda: datetime.now(tz)

dt0 = datetime(1995, 11, 11, 11, 11, 11, 111111, tz)

# 从datetime获取时间戳
ts0 = dt0.timestamp()

# 从时间戳获取datetime
dt_from_ts = datetime.fromtimestamp(ts0)

timepattern = "%Y-%m-%d %H:%M:%S.%f"
# 从datetime获取datetime字符串
t0 = dt0.strftime(timepattern)

# 从datetime字符串获取datetime
dt_from_t0 = datetime.strptime(t0, timepattern)

你可能感兴趣的:(Python datetime模块的一些简单使用)