python-日期

#!/usr/bin/python
#-*- coding:utf-8 -*-
# author: sirong.zhu
# create date: 2016.09.08
import calendar
import datetime
import time
from datetime import timedelta, date

class dateutils:


    '''
        获取上月日期(具体哪一天可通过参数day自定义,默认是1号)
    '''
    @staticmethod
    def query_last_month_day(day=None):
        '''
        :param day:
        '''
        if(None == day):
            day = 1
        return (datetime.date.today().replace(day=1) - datetime.timedelta(1)).replace(day=day)

    '''
        获取上月日期(具体哪一天可通过参数day自定义,默认是1号)
    '''
    @staticmethod
    def query_last_month_daystr(day=None,month_format="%Y-%m-%d"):
        '''
        '''
        if(None == day):
            day = 1
        return datetime.datetime.strftime((datetime.date.today().replace(day=1) - datetime.timedelta(1)).replace(day=day), month_format)

    '''
        获取当月日期(具体哪一天可通过参数day自定义,默认是1号)
    '''
    @staticmethod
    def query_this_month_day(day=None):
        '''
        :param day:21
        '''
        if(None == day):
            day = 1
        return (datetime.date.today().replace(day=1)).replace(day=day)

    '''
        获取当月日期(具体哪一天可通过参数day自定义,默认是1号)
    '''
    @staticmethod
    def query_this_month_daystr(day=None,month_format="%Y-%m-%d"):
        '''
        :param day:21
        :month_format:"%Y-%m-%d"
        '''
        if(None == day):
            day = 1
        return datetime.datetime.strftime((datetime.date.today().replace(day=1)).replace(day=day), month_format)


    '''
        字符串转成日期,时分秒为都0
    '''
    @staticmethod
    def convert_str_to_date(expire_time):
        '''
        :param expire_time:2013-05-21 21:21:21
        :return:
        '''
        _inner_expire_time = expire_time
        if(len(expire_time) == 8):
            _inner_expire_time = str(datetime.datetime.strptime(expire_time, "%Y%m%d")).replace(" 00:00:00","")
        d = datetime.datetime.strptime(_inner_expire_time,"%Y-%m-%d")
        return d

    '''
        根据日期时间获取对应的月份
    '''
    @staticmethod
    def query_datestr_by_datetime(expire_time,month_format):
        '''
        :param expire_time:2013-05-21 21:21:21
        :param month_format:%Y-%m
        :return:
        '''
        d = datetime.datetime.strptime(expire_time,"%Y-%m-%d %H:%M:%S")
        return datetime.datetime.strftime(d, month_format)

    '''
        获取今天日期
    '''
    @staticmethod
    def query_today(day_format):
        ISOTIMEFORMAT = '%Y-%m-%d %H:%M:%S'
        if None != day_format:
            ISOTIMEFORMAT = day_format
        return time.strftime( ISOTIMEFORMAT, time.localtime())

    '''
        字符串转成日期-时间
    '''
    @staticmethod
    def convert_str_to_datetime(expire_time):
        '''
        :param expire_time:
        :return:
        '''
        d = datetime.datetime.strptime(expire_time,"%Y-%m-%d %H:%M:%S")
        return d

    '''
        字符串转成日期-时间
    '''
    @staticmethod
    def query_last_month_last_day(date_format):
        '''
        :param date_format:
        :return:
        '''
        last_month_last_day = datetime.date(datetime.date.today().year,datetime.date.today().month,1)-datetime.timedelta(1)
        return datetime.datetime.strftime(last_month_last_day, date_format)

    '''
        字符串转成日期-时间
    '''
    @staticmethod
    def query_this_month_last_day(date_format):
        '''
        :param date_format:
        :return:
        '''
        FORMAT = "%d-%d-%d 00:00:00"
        year = datetime.date.today().year
        month = datetime.date.today().month
        d = calendar.monthrange(year, month)
        return datetime.datetime.strftime(datetime.datetime.strptime(FORMAT % (year, month, d[1]), "%Y-%m-%d %H:%M:%S"),date_format)

    '''
        获取传入日期的最小、最大时间
    '''
    @staticmethod
    def query_min_max_time(dateStr):
        min_time = str(dateutils.convert_str_to_date(dateStr))
        max_time = str(dateutils.convert_str_to_date(dateStr)).replace("00:00:00","23:59:59")
        return (min_time,max_time)

    '''
        if n>=0,date is larger than today
        if n<0,date is less than today
        date format = "YYYY-MM-DD"
    '''
    @staticmethod
    def query_day_of_day(n=0):
        if(n<0):
            n = abs(n)
            return date.today()- timedelta(days=n)
        else:
            return date.today()+timedelta(days=n)


if __name__=="__main__":
        # print dateutils.query_this_month_daystr(21)
        print dateutils.convert_str_to_date("20130521")
        # print dateutils.query_this_month_day(12)
        # print dateutils.query_datestr_by_datetime("2013-05-21 21:21:21","%Y%m")
        # print dateutils.query_last_month_last_day("%Y%m%d")
        # print dateutils.query_this_month_last_day("%Y%m%d")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值