python datetime 与时间戳相互转换
2017-02-21 16:03
537 查看
python datetime 与时间戳相互转换
首先需要引入的时间相关模块:import time, datetime
from datetime import date, datetime, timedelta
字符串时间online_time变为datetime类型online_dt:
DT_FORMAT='%Y%m%d %H:%M:%S'
online_time = '20170219 00:02:07'
online_dt = datetime.strptime(online_time, DT_FORMAT)
datetime类型online_dt变为时间戳秒online_seconds:
online_seconds = int(time.mktime(online_dt.timetuple()))
时间戳秒online_seconds变为datetime类型online_dt2(忽略时区):
online_dt2 = datetime.fromtimestamp(online_seconds)
datetime类型online_dt2变为字符串时间online_time2:
online_time2=online_dt2.strftime(DT_FORMAT)显然online_time==online_time2。形成完成变换链。
相关文章推荐
- python时间模块time,时间戳,结构化时间,字符串时间,相互转换,datetime
- python 使用datetime进行时间日期转换
- Python中time模块和datetime模块的常用操作以及几种常用时间格式间的转换
- python中date、datetime、string的相互转换
- python 时间戳 datetime string 转换
- python datetime时间格式 与 时间字符串 相互转化
- Python 调用datetime或者time获取时间的时候以及时间转换,最好设置一下时区 否则会出现相差8个小时的情况
- Python字符串、时间戳、datetime时间相关转换
- python中date、datetime、string的相互转换
- python中date、datetime、string的相互转换
- python中date、datetime、string的相互转换
- Python- 时间格式的相互转换
- Python中String与时间戳、datetime转换
- data_analysis(二)时间戳、日期等各种日期型python相互转换
- python中date、datetime、string的相互转换
- c#之时间戳与DateTime的相互转换
- Python中时间戳与时间字符串相互转换
- Python中实现对Timestamp和Datetime及UTC时间之间的转换
- python 时间类型和相互转换
- 【python】----- time,datetime,string相互转换