通过调用下面的方法获取想要获得的时间,时间格式为yyyy-MM-dd
# encoding: utf-8
import datetime
def get_curmonth_firstday():
"""
获取当月第一天
:return:
"""
return datetime.date(datetime.date.today().year,datetime.date.today().month,1)
def get_lastmonth_lastday():
"""
获取上月最后一天
:return:
"""
return get_curmonth_firstday() - datetime.timedelta(days=1)
def get_lastmonth_firstday():
"""
获取上月第一天
:return:
"""
return datetime.date(get_lastmonth_lastday().year, get_lastmonth_lastday().month, 1)
def get_premonth_firstday(num):
"""
获取前几个月第一天
:param num: 月份数
:return:
"""
i = 1
day = get_curmonth_firstday()
while(i <= num):
day = day - datetime.timedelta(days=1)
day = datetime.date(day.year, day.month, 1)
i = i + 1
return day
def get_premonth_lastday(num):
"""
获取前几个月最后一天
:param num: 月份数
:return:
"""
i = 1
day = get_curmonth_firstday()
while(i <= num):
day = datetime.date(day.year, day.month, 1)
day = day - datetime.timedelta(days=1)
i = i + 1
return day
def get_preday(num):
"""
获取前几天
:param num: 天数
:return:
"""
i = 1
while(i <= num):
today = datetime.date.today()
day = today - datetime.timedelta(days=num)
i = i + 1
return day
def get_today():
"""
获取今天
:return:
"""
return datetime.date.today()
def get_prespcday(spcday, num):
"""
获取前几天
:param num: 天数
:return:
"""
i = 1
while(i <= num):
spcday1 = datetime.datetime.strptime(spcday, "%Y-%m-%d")
day = spcday1 - datetime.timedelta(days=num)
i = i + 1
return str(day).split(' ')[0]
def get_nextspcday(spcday, num):
"""
获取前几天
:param num: 天数
:return:
"""
i = 1
while(i <= num):
spcday1 = datetime.datetime.strptime(spcday, "%Y-%m-%d")
day = spcday1 + datetime.timedelta(days=num)
i = i + 1
return str(day).split(' ')[0]