最近工作要用到Python 写web,看着文档整理了一下过程中处理时间的笔记
Python时间操作
time 模块 操作时间
datetime 模块 操作日期
时间转换主要包括:
① 时间戳 ---> 字符串显示时间格式(XXXX年-xx月-xx日 时:分:秒)
② 字符串显示时间格式(XXXX年-xx月-xx日 时:分:秒) ---> 时间戳
time模块处理
获取时间戳 time.time()
时间戳转字符串 time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
字符串转时间戳 time.strptime('2017-08-14 00:00:00', '%Y-%m-%d %H:%M:%S') 返回 time.struct_time
time.mktime(time.struct_time) 时间戳
datetime 模块处理
datetime 类
datetime.strptime('2017-08-14 00:00:00', '%Y-%m-%d %H:%M:%S') 静态函数 将标准字符串转为 datetime 对象
strftime('%Y-%m-%d %H:%M:%S') 成员函数 将datetime对象 返回一个标准字符串
time模块将时间戳 转化成 标准日期字符串 然后 用 datetime模块将该串 解析为 一个 datetime 对象
通过datetime对象 做 +1天 -1天 等 日期处理后 再转化为一个 日期串
time模块在将 日期串 转化为 时间戳
以上 就可以随便处理日期了