end_time = datetime.date.today()
year = end_time.strftime("%Y")
month = end_time.strftime("%m")
day = end_time.strftime("%d")
params = {
"year": year,
"month": month,
"day": day,
"current_date": f"{year}-{month}-{day}"
return params
def get_current_week():
:return:
start_time, end_time = datetime.date.today(), datetime.date.today()
one_day = datetime.timedelta(days=1)
while start_time.weekday() != 0:
start_time -= one_day
while end_time.weekday() != 6:
end_time += one_day
start_year = start_time.strftime("%Y")
start_month = start_time.strftime("%m")
start_day = start_time.strftime("%d")
year = end_time.strftime("%Y")
month = end_time.strftime("%m")
end_day = end_time.strftime("%d")
params = {
"start_year": start_year, # 开始年份
"start_month": start_month, # 开始月份
"start_day": start_day, # 开始日期
"start_date": f"{start_year}-{start_month}-{start_day}", # 开始日期
"end_year": year, # 结束年份
"end_month": month, # 结束月份
"end_day": end_day, # 结束日期
"end_date": f"{year}-{month}-{end_day}" # 结束日期
return params
def get_current_month():
当前时间的月份首日与最后一天
:return:
today = datetime.date.today()
year, month = today.year, today.month
end = datetime.datetime(year, month + 1, 1) - datetime.timedelta(days=1)
params = {
"year": year,
"month": month,
"start_day": "01",
"start_date": f"{year}-{month}-01",
"end_day": f"{end.day}",
"end_date": f"{year}-{month}-{end.day}",
return params
def get_last_day():
:return:
d = datetime.date.today()
one_day = datetime.timedelta(days=1)
end_time = d - one_day
year = end_time.strftime("%Y")
month = end_time.strftime("%m")
day = end_time.strftime("%d")
params = {
"year": year,
"month": month,
"day": day,
"last_date": f"{year}-{month}-{day}"
return params
def get_last_week():
:return:
d = datetime.date.today()
days_count = datetime.timedelta(days=d.isoweekday())
end_time = d - days_count
six_days = datetime.timedelta(days=6)
start_time = end_time - six_days
start_year = start_time.strftime("%Y")
start_month = start_time.strftime("%m")
start_day = start_time.strftime("%d")
year = end_time.strftime("%Y")
month = end_time.strftime("%m")
end_day = end_time.strftime("%d")
params = {
"start_year": start_year,
"start_month": start_month,
"start_day": start_day,
"start_date": f"{start_year}-{start_month}-{start_day}",
"year": year,
"month": month,
"end_day": end_day,
"end_date": f"{year}-{month}-{end_day}",
return params
def get_last_month():
d = datetime.date.today()
days_count = datetime.timedelta(days=d.day)
end_time = d - days_count
year = end_time.strftime("%Y")
month = end_time.strftime("%m")
params = {
"year": year,
"month": month,
"start_day": "01",
"start_date": f"{year}-{month}-01",
"end_day": f"{end_time.day}",
"end_date": f"{year}-{month}-{end_time.day}",
return params
if __name__ == '__main__':
print('当日', get_current_day())
print('当周', get_current_week())
print('当月', get_current_month())
print('昨日', get_last_day())
print('上周', get_last_week())
print('上月', get_last_month())
获取当前月、上一个月、下一个月、月份第一天、月份最后一天
import calendar
from datetime import datetime
from dateutil.relativedelta import relativedelta
class DateTimeUtil():
def get_cur_month(self):
# 获取当前月
return datetime.now().strftime("%Y-%m")
def get_last_month(self, number=1):
# 获取前几个月
month_date = datetime.now().date() - relativedelta(months=number)
return month_date.strftime("%Y-%m")
def get_next_month(self, number=1):
# 获取后几个月
month_date = datetime.now().date() + relativedelta(months=number)
return month_date.strftime("%Y-%m")
def get_cur_month_start(self):
# 获取当前月的第一天
month_str = datetime.now().strftime('%Y-%m')
return '{}-01'.format(month_str)
def get_cur_month_end(self):
# 获取当前月的最后一天
param: month_str 月份,2021-04
# return: 格式 %Y-%m-%d
month_str = datetime.now().strftime('%Y-%m')
year, month = int(month_str.split('-')[0]), int(month_str.split('-')[1])
end = calendar.monthrange(year, month)[1]
return '{}-{}-{}'.format(year, month, end)
def get_last_month_start(self, month_str=None):
# 获取上一个月的第一天
param: month_str 月份,2021-04
# return: 格式 %Y-%m-%d
if not month_str:
month_str = datetime.now().strftime('%Y-%m')
year, month = int(month_str.split('-')[0]), int(month_str.split('-')[1])
if month == 1:
year -= 1
month = 12
else:
month -= 1
return '{}-{}-01'.format(year, month)
def get_next_month_start(self, month_str=None):
# 获取下一个月的第一天
param: month_str 月份,2021-04
# return: 格式 %Y-%m-%d
if not month_str:
month_str = datetime.now().strftime('%Y-%m')
year, month = int(month_str.split('-')[0]), int(month_str.split('-')[1])
if month == 12:
year += 1
month = 1
else:
month += 1
return '{}-{}-01'.format(year, month)
def get_last_month_end(self, month_str=None):
# 获取上一个月的最后一天
param: month_str 月份,2021-04
# return: 格式 %Y-%m-%d
if not month_str:
month_str = datetime.now().strftime('%Y-%m')
year, month = int(month_str.split('-')[0]), int(month_str.split('-')[1])
if month == 1:
year -= 1
month = 12
else:
month -= 1
end = calendar.monthrange(year, month)[1]
return '{}-{}-{}'.format(year, month, end)
def get_next_month_end(self, month_str=None):
# 获取下一个月的最后一天
param: month_str 月份,2021-04
# return: 格式 %Y-%m-%d
if not month_str:
month_str = datetime.now().strftime('%Y-%m')
year, month = int(month_str.split('-')[0]), int(month_str.split('-')[1])
if month == 12:
year += 1
month = 1
else:
month += 1
end = calendar.monthrange(year, month)[1]
return '{}-{}-{}'.format(year, month, end)
if __name__ == '__main__':
# 获取当前月
print('当前月', DateTimeUtil().get_cur_month())
# 获取上一个月
print('上一个月', DateTimeUtil().get_last_month())
# 获取上两个月
print('上两个月', DateTimeUtil().get_last_month(number=2))
# 获取下一个月
print('下一个月', DateTimeUtil().get_next_month())
# 获取下两个月
print('下两个月', DateTimeUtil().get_next_month(number=2))
# 获取当前月的第一天
print('当前月的第一天', DateTimeUtil().get_cur_month_start())
# 获取当前月的最后一天
print('当前月的最后一天', DateTimeUtil().get_cur_month_end())
# 获取上个月的第一天
print('上个月的第一天', DateTimeUtil().get_last_month_start())
# 获取下个月的第一天
print('下个月的第一天', DateTimeUtil().get_next_month_start())
# 获取上个月的最后一天
print('上个月的最后一天', DateTimeUtil().get_last_month_end())
# 获取下个月的最后一天
print('下个月的最后一天', DateTimeUtil().get_next_month_end())
输出:
当前月 2021-04
上一个月 2021-03
上两个月 2021-02
下一个月 2021-05
下两个月 2021-06
当前月的第一天 2021-04-01
当前月的最后一天 2021-4-30
上个月的第一天 2021-3-01
下个月的第一天 2021-5-01
上个月的最后一天 2021-3-31
下个月的最后一天 2021-5-31