Python-时间处理

1. 时间字符串 --> 时间戳


timestr= '2017-06-06 10:00:00'
seconds =  time.mktime(time.strptime(timestr, '%Y-%m-%d %H:%M:%S'))

将时间元组转换成时间戳
time.mktime(timetuple) :


将时间戳转会为时间元组

time.localtime([timestamp]):


 

2. 时间戳 --> 时间字符串

1) time 模块


timestamp = time.time()
timestruct = time.localtime(timestamp)
timestr = time.strftime('%Y-%m-%d %H:%M:%S', timestruct)

    

2) datetime 模块

import datetime
timestamp = 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值