Python教程

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

本文主要是介绍python获取本天,本周,本月,本年,上一天,上一周,上一月,上一年的开始及结束日期,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

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'}
"""

这篇关于python获取本天,本周,本月,本年,上一天,上一周,上一月,上一年的开始及结束日期的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!