ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

python获取本天,本周,本月,本年,上一天,上一周,上一月,上一年的开始及结束日期

2021-09-29 16:34:26  阅读:141  来源: 互联网

标签:end python 上一年 本天 start year date datetime today


python环境为python3.6.5

import datetime
import calendar


def get_current_day():
	'''
	当日
	:return:

	'''
	today = datetime.datetime.today()
	return {"start_date":today.strftime("%Y-%m-%d"),"end_date": today.strftime("%Y-%m-%d")}


def get_current_week():
	''' 当周 '''
	today = datetime.date.today()
	start_date = today - datetime.timedelta(days=today.weekday())
	end_date = today + datetime.timedelta(days=(6-today.weekday()))
	return {"start_date": start_date.strftime("%Y-%m-%d"), "end_date": end_date.strftime("%Y-%m-%d")}



def get_current_month():
	''' 当月'''
	today = datetime.datetime.today()
	year, month = today.year, today.month
	days = calendar.monthrange(year, month)[1]
	params = {"start_date": datetime.datetime.strftime(datetime.datetime.strptime(f"{year}-{month}-01", "%Y-%m-%d"), "%Y-%m-%d"),
			  "end_date": datetime.datetime.strftime(datetime.datetime.strptime(f"{year}-{month}-{days}", "%Y-%m-%d"), "%Y-%m-%d"), }
	return params

def get_current_year():
	''' 当年'''
	today = datetime.datetime.today()
	year = today.year
	days = calendar.monthrange(year, 12)[1]
	params = {"start_date": f"{year}-01-01",  "end_date": f"{year}-12-{days}"}
	return params


def get_last_day():
	'''上一天 '''
	today = datetime.datetime.today()
	one_day = datetime.timedelta(days=1)
	end_time = today - one_day
	year = end_time.strftime("%Y")
	month = end_time.strftime("%m")
	day = end_time.strftime("%d")
	params = {"start_date": f"{year}-{month}-{day}", "end_date": f"{year}-{month}-{day}" }
	return params


def get_last_week():
	''' 上一周 '''
	today = datetime.datetime.today()
	days_count = datetime.timedelta(days=today.isoweekday())
	end_time = today - 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_date": f"{start_year}-{start_month}-{start_day}", "end_date": f"{year}-{month}-{end_day}"}
	return params


def get_last_month():
	"""上一月"""
	today = datetime.datetime.today()
	days_count = datetime.timedelta(days=today.day)
	end_time = today - days_count
	year = end_time.strftime("%Y")
	month = end_time.strftime("%m")
	params = {"start_date": f"{year}-{month}-01", "end_date": f"{year}-{month}-{end_time.day}"}
	return params


def get_last_year():
	"""上一年"""
	today = datetime.datetime.today()
	year = today.year - 1
	days = calendar.monthrange(year, 12)[1]
	params = {"start_date": f"{year}-01-01", "end_date": f"{year}-12-{days}"}
	return params


if __name__ == '__main__':
	print('当日', get_current_day())
	print('当周', get_current_week())
	print('当月', get_current_month())
	print('当年', get_current_year())
	print('上一日', get_last_day())
	print('上一周', get_last_week())
	print('上一月', get_last_month())
	print('上一年', get_last_year())

"""运行结果:
当日 {'start_date': '2021-09-29', 'end_date': '2021-09-29'}
当周 {'start_date': '2021-09-27', 'end_date': '2021-10-03'}
当月 {'start_date': '2021-09-01', 'end_date': '2021-09-30'}
当年 {'start_date': '2021-01-01', 'end_date': '2021-12-31'}
上一日 {'start_date': '2021-09-28', 'end_date': '2021-09-28'}
上一周 {'start_date': '2021-09-20', 'end_date': '2021-09-26'}
上一月 {'start_date': '2021-08-01', 'end_date': '2021-08-31'}
上一年 {'start_date': '2020-01-01', 'end_date': '2020-12-31'}
"""

标签:end,python,上一年,本天,start,year,date,datetime,today
来源: https://blog.csdn.net/time_money/article/details/120551139

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有