#!/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")
python-日期
最新推荐文章于 2021-09-29 20:37:44 发布