Python-获取当前月的前x月月份日期
程序员文章站
2024-01-02 11:43:04
"""获取当前月的前x月月份日期"""import datetimedef getTheMonth(date, n): month = date.month year = date.year for i in range(n): if month == 1: year -= 1 month = 12 else: month -= 1 return datetim....
"""
获取当前月的前x月月份日期
"""
import datetime
def getTheMonth(date, n):
month = date.month
year = date.year
for i in range(n):
if month == 1:
year -= 1
month = 12
else:
month -= 1
return datetime.date(year, month, 1).strftime('%Y-%m')
date = datetime.datetime.today()
single_time_new = getTheMonth(date, 5)
single_time_old = getTheMonth(date, 6)
import datetime
# 根据开始日期、结束日期返回这段时间里所有天的集合
def getDatesByTimes(sDateStr, eDateStr):
list = []
datestart = datetime.datetime.strptime(sDateStr, '%Y-%m-%d')
dateend = datetime.datetime.strptime(eDateStr, '%Y-%m-%d')
list.append(datestart.strftime('%Y-%m-%d'))
while datestart < dateend:
datestart += datetime.timedelta(days=1)
list.append(datestart.strftime('%Y-%m-%d'))
return list
#上一天的时间
def getYesterday():
yesterday = datetime.date.today() + datetime.timedelta(-2)
return yesterday
#现在时间
def getnowYesterday():
nowday = datetime.date.today() + datetime.timedelta(-1)
return nowday
#获取前1天或N天的日期,beforeOfDay=1:前1天;beforeOfDay=N:前N天
def getdate(beforeOfDay):
today = datetime.datetime.now()
# 计算偏移量
offset = datetime.timedelta(days=-beforeOfDay)
# 获取想要的日期的时间
re_date = (today + offset).strftime('%Y-%m-%d')
return re_date
#获取前一周的所有日期(weeks=1),获取前N周的所有日期(weeks=N)
def getBeforeWeekDays(weeks=1):
# 0,1,2,3,4,5,6,分别对应周一到周日
weeks=1
week = datetime.datetime.now().weekday()
days_list = []
start = 7 * weeks + week
end = week
for index in range(start, end, -1):
day =getdate(index)
days_list.append(day)
return days_list
#计算两个日期相差天数,自定义函数名,和两个日期的变量名。
def Caltime(date1,date2):
#%Y-%m-%d为日期格式,其中的-可以用其他代替或者不写,但是要统一,同理后面的时分秒也一样;可以只计算日期,不计算时间。
#date1=time.strptime(date1,"%Y-%m-%d %H:%M:%S")
#date2=time.strptime(date2,"%Y-%m-%d %H:%M:%S")
date1=time.strptime(date1,"%Y/%m/%d")
date2=time.strptime(date2,"%Y/%m/%d")
#根据上面需要计算日期还是日期时间,来确定需要几个数组段。下标0表示年,小标1表示月,依次类推...
#date1=datetime.datetime(date1[0],date1[1],date1[2],date1[3],date1[4],date1[5])
#date2=datetime.datetime(date2[0],date2[1],date2[2],date2[3],date2[4],date2[5])
date1=datetime.datetime(date1[0],date1[1],date1[2])
date2=datetime.datetime(date2[0],date2[1],date2[2])
#返回两个变量相差的值,就是相差天数
print((date2-date1).days)#将天数转成int型
return(date2-date1)
from datetime import timedelta
import datetime
import calendar
def gen_dates(b_date, days):
day = timedelta(days=1)
for i in range(days):
yield b_date + day*i
def get_date_list(start_date, end_date):
if start_date is not None:
start = datetime.datetime.strptime(start_date, "%Y-%m-%d")
if end_date is None:
end = datetime.datetime.now()
else:
end = datetime.datetime.strptime(end_date, "%Y-%m-%d")
data = []
for d in gen_dates(start, ((end-start).days + 1)): # 29 + 1
data.append(d.strftime("%Y-%m-%d"))
return data
def getMonthFirstDayAndLastDay(year=None, month=None):
if year:
year = int(year)
else:
year = datetime.date.today().year
if month:
month = int(month)
else:
month = datetime.date.today().month
# 获取当月第一天的星期和当月的总天数
firstDayWeekDay, monthRange = calendar.monthrange(year, month)
# 获取当月的第一天
firstDay = datetime.date(year=year, month=month, day=1)
firstDay = firstDay.strftime('%Y-%m-%d')
lastDay = datetime.date(year=year, month=month, day=monthRange)
lastDay = lastDay.strftime('%Y-%m-%d')
return firstDay,lastDay
def first_last(time):
year = time[:4]
month = time[4:6]
firstday = getMonthFirstDayAndLastDay(year, month)[0]
lastday = getMonthFirstDayAndLastDay(year, month)[1]
return get_date_list(firstday,lastday)
def main():
time="202002"
print(first_last(time))
本文地址:https://blog.csdn.net/qq_39142369/article/details/110874495