下方为python中对时间数据操作的常见需求案例,请分清传参格式后进行选择摘取:
1、传参为datetime格式:
该段代码中封装了时间的年月日获取方法、获取上月及下月的月份方法、上月本月下月月份中所对应的天数、获取当天开始和结束日期、根据日期算出未来某天是周几、计算时间差等等方式,可根据需要自行选择:
from datetime import datetime, timedelta
import calendar
# 1. 获取当年、当月、当日
date = datetime(2024, 8, 11)
year = date.year
month = date.month
day = date.day
print(f"Year: {year}, Month: {month}, Day: {day}")
# 2. 获取本月的上个月和下个月的月份
prev_month = date.replace(day=1) - timedelta(days=1)
next_month = (date.replace(day=28) + timedelta(days=4)).month
print(f"Previous Month: {prev_month.month}, Next Month: {next_month}")
# 3. 获取上个月、本月、下个月的天数
days_in_prev_month = calendar.monthrange(prev_month.year, prev_month.month)[1]
days_in_current_month = calendar.monthrange(date.year, date.month)[1]
days_in_next_month = calendar.monthrange(date.year, next_month)[1]
print(f"Days in Previous Month: {days_in_prev_month}")
print(f"Days in Current Month: {days_in_current_month}")
print(f"Days in Next Month: {days_in_next_month}")
# 4. 获取当日的开始时间和结束时间
start_of_day = datetime(date.year, date.month, date.day, 0, 0, 0)
end_of_day = datetime(date.year, date.month, date.day, 23, 59, 59)
print(f"Start of Day: {start_of_day}")
print(f"End of Day: {end_of_day}")
# 5. 获取当日几天后是星期几(days=为几天以后)
future_date = date + timedelta(days=5)
weekday = future_date.strftime("%A")
print(f"5 Days Later: {weekday}")
# 6.获取两个日期之间相差的天数
date1 = datetime(2024,8,1)
date2 = datetime(2024,12,21)
delta = date2 - date1
print(f"Difference in days: {delta.days}")
2、传参为string格式:
该段代码主要是对字符串格式的日期进行加工操作,下列代码中同时包含了上述操作代码以及额外新增处理日期特殊格式需求;
from datetime import datetime, timedelta
import calendar
# 假设日期是字符串
date_str = "2024-08-11"
# 将字符串转换为 datetime 对象
date = datetime.strptime(date_str, "%Y-%m-%d")
# 1. 获取当年、当月、当日
year = date.year
month = date.month
day = date.day
print(f"Year: {year}, Month: {month}, Day: {day}")
# 2. 获取本月的上个月和下个月的月份
prev_month = date.replace(day=1) - timedelta(days=1)
next_month = (date.replace(day=28) + timedelta(days=4)).month
print(f"Previous Month: {prev_month.month}, Next Month: {next_month}")
# 3. 获取上个月、本月、下个月的天数
days_in_prev_month = calendar.monthrange(prev_month.year, prev_month.month)[1]
days_in_current_month = calendar.monthrange(date.year, date.month)[1]
days_in_next_month = calendar.monthrange(date.year, next_month)[1]
print(f"Days in Previous Month: {days_in_prev_month}")
print(f"Days in Current Month: {days_in_current_month}")
print(f"Days in Next Month: {days_in_next_month}")
# 4. 获取当日的开始时间和结束时间
start_of_day = datetime(date.year, date.month, date.day, 0, 0, 0)
end_of_day = datetime(date.year, date.month, date.day, 23, 59, 59)
print(f"Start of Day: {start_of_day}")
print(f"End of Day: {end_of_day}")
# 5. 获取当日5天后是星期几
future_date = date + timedelta(days=5)
weekday = future_date.strftime("%A")
print(f"5 Days Later: {weekday}")
#6.计算两时间之间的相差天数
date_str1 = "2024-04-22"
date_str2 = "2024-12-29"
date1 = datetime.strptime(date_str1, "%Y-%m-%d")
date2 = datetime.strptime(date_str2, "%Y-%m-%d")
delta = date2 - date1
print(f"Difference in days: {delta.days}")
# 7.去掉日期之间的‘-’,并取日期的月份计算两日期之间相差的月份
date_str1 = "2024-04-22"
date_str2 = "2024-12-29"
date1 = datetime.strptime(date_str1, "%Y-%m-%d")
date2 = datetime.strptime(date_str2, "%Y-%m-%d")
months_diff = (date2.year - date1.year) * 12 + (date2.month - date1.month)
formatted_date1 = date1.strftime("%Y%m")
formatted_date2 = date2.strftime("%Y%m")
print(f"Formatted Dates: {formatted_date1} to {formatted_date2}")
print(f"Difference in months: {months_diff}")