Python-时间处理
来源:互联网 发布:linux解压gz文件命令 编辑:程序博客网 时间:2024/06/05 00:14
1. 时间字符串 --> 时间戳
seconds = time.mktime(time.strptime(timestr, '%Y-%m-%d %H:%M:%S'))
将时间元组转换成时间戳
time.mktime(timetuple) :
将时间戳转会为时间元组
2. 时间戳 --> 时间字符串
1) time 模块
timestamp = time.time()
timestruct = time.localtime(timestamp)
timestr = time.strftime('%Y-%m-%d %H:%M:%S', timestruct)
2) datetime 模块
import datetimetimestamp = 1482374997.55
datetime_struct = datetime.datetime.fromtimestamp(timestamp)
print datetime_struct.strftime('%Y-%m-%d %H:%M:%S')
>> 2016-12-22 10:49:57
datetime_struct = datetime.datetime.utcfromtimestamp(timestamp)
print datetime_struct.strftime('%Y-%m-%d %H:%M:%S')
>> 2016-12-22 02:49:57
fromtimestamp(timestamp[, tz]):将时间戳转为当地的时间元组
utcfromtimestamp(timestamp):将时间戳转为UTC的时间元组。以北京为例:utc时间比北京当地时间少8个小时。
3. 时间差计算
def preday_get(d):
"""
返回上一天的日期时间
:return
date_from: 2017-05-01 00:00:00
date_to: 2017-05-01 23:59:59
"""
oneday = datetime.timedelta(days=1)
day = d - oneday
date_from = datetime.datetime(day.year, day.month, day.day, 0, 0, 0)
date_to = datetime.datetime(day.year, day.month, day.day, 23, 59, 59)
# print '---'.join([str(date_from), str(date_to)])
return date_from, date_to
def preweek_get(d):
"""
返回上一周的日期时间
:return
date_from: 2017-05-01 00:00:00
date_to: 2017-05-07 23:59:59
"""
dayscount = datetime.timedelta(days=d.isoweekday())
dayto = d - dayscount
sixdays = datetime.timedelta(days=6)
dayfrom = dayto - sixdays
date_from = datetime.datetime(dayfrom.year, dayfrom.month, dayfrom.day,0, 0, 0)
date_to = datetime.datetime(dayto.year, dayto.month, dayto.day,23, 59, 59)
# print '---'.join([str(date_from), str(date_to)])
return date_from, date_to
def premonth_get(d):
"""
返回上个月第一个天和最后一天的日期时间
:return
date_from: 2017-04-01 00:00:00
date_to: 2017-04-30 23:59:59
"""
dayscount = datetime.timedelta(days=d.day)
dayto = d - dayscount
date_from = datetime.datetime(dayto.year, dayto.month, 1, 0, 0, 0)
date_to = datetime.datetime(dayto.year, dayto.month, dayto.day,23, 59, 59)
# print '---'.join([str(date_from), str(date_to)])
return date_from, date_to
def nextmonth_get(d):
"""
返回下个月第一个天
:return date: 2017-06-01 00:00:00
"""
date = datetime.datetime(d.year, d.month+1,1, 0, 0, 0)
# print '---'.join([str(date_from), str(date_to)])
return date
def nextweek_get(d):
"""
返回下周第一个天
:return date: 2017-06-01 00:00:00
"""
dayscount = datetime.timedelta(days=d.isoweekday())
dayfrom = d - dayscount
stepdate = datetime.timedelta(days=8)
dayto = dayfrom + stepdate
date = datetime.datetime(dayto.year, dayto.month, dayto.day, 00, 00, 00)
return date
4. 任意时间字符串转换时间对象
import time
from dateutil import parser
time_string = time.ctime() # 'Thu Dec 22 10:35:25 2016',这里可以是任意的时间格式
datetime_struct = parser.parse(time_string)
print type(datetime_struct) # <type 'datetime.datetime'>
print datetime_struct.strftime('%Y-%m-%d %H:%M:%S') # 2016-12-22 13:58:59
- python datetime时间处理
- Python 时间处理
- python 时间处理
- python datetime处理时间
- python UNIX_TIMESTAMP时间处理
- python datetime处理时间
- python时间处理函数
- A005:python 时间处理
- python 时间戳处理
- python 时间戳处理
- python 时间处理
- python 时间处理
- python datetime处理时间
- python datetime处理时间
- python处理时间
- python-时间处理问题
- Python 时间处理
- python 时间处理
- socket服务器
- hihocoder 字符消除
- AES加密解密
- Java学习笔记---反射
- Android-times-square自定义样式
- Python-时间处理
- 如何免费使用webstorm
- junit的错误 No tests found matching [{ExactMatcher:fDisplayName=test2],
- RMI远程服务操作
- Jsonp实现跨域详解
- js Date 获取 年 月 日 具体的值
- java web中servlet实现post方法上传多文件数据接收
- 用vue,如何给一个列表中的每个元素标签绑定id
- Kafka 配置使用注意事项