python 数据处理 - datetime模块
1.datetime模块介绍
类名 | 功能说明 |
---|---|
date | 日期对象,常用的属性有year, month, day |
time | 时间对象 |
datetime | 日期时间对象,常用的属性有hour, minute, second, microsecond |
datetime_CAPI | 日期时间对象C语言接口 |
timedelta | 时间间隔,即两个时间点之间的长度 |
tzinfo | 时区信息对象 |
2.datetime各模块分别介绍
https://www.cnblogs.com/awakenedy/articles/9182036.html
3.strftime和strptime
# 由日期格式转化为字符串格式的函数为: datetime.datetime.strftime()
# 由字符串格式转化为日期格式的函数为: datetime.datetime.strptime()
from datetime import datetime # 导入datetime模块的datetime类
# 示例1
s=datetime.strftime(datetime.now(), “%Y-%m-%d %H:%M”)
# 示例2
cday = datetime.strptime('2017-8-1 18:20:20', '%Y-%m-%d %H:%M:%S')
# 示例3 - 转换时间戳格式
import time
timeStamp = 1588287405000
for i in range(100):
dateArray = time.localtime(data.loc[i,'created'] / 1000)
otherStyleTime = time.strftime("%Y--%m--%d %H:%M:%S", dateArray)
print(otherStyleTime)