如何使用Python确定上周五的日期?
在这篇文章中,我们将学习如何使用Python确定上周五的日期。
使用的方法
以下是完成此任务的各种方法:
使用 datetime 模块
使用 dateutil 包
方法1:使用 Datetime 模块
算法(步骤)
以下是执行所需任务的算法/步骤:
使用 import 关键字从 datetime 模块导入 **datetime, timedelta**。
创建一个变量来存储一周中的所有日期 **(weekdays)**。
创建一个函数 **getPreviousByDay()**,通过接受输入日期、start_date=None作为参数来返回输入日期的最后一天。
使用 **if 条件** 语句检查开始日期是否为 None。
将今天的日期(当前日期)分配给开始日期。
使用 datetime 模块的 **weekday()** 函数(返回与给定一周中的某一天对应的整数)获取当前星期几的数字。
使用 index() 函数获取目标星期几(给定的输入日期)的索引。
获取上周的天数。
使用 **if 条件** 语句检查上述 days_ago 变量的结果是否等于 0,如果是,则将 7 分配给 days_ago 变量。
否则,从当前日期(开始日期)减去上述天数,以获取给定日期上周的日期。
返回输入日期上周的日期。
使用 datetime 的 today() 函数打印今天的/当前日期和时间。
通过将 inputDay 作为 'Friday' 传递给上面定义的 **getPreviousByDay()** 函数来打印上周五的日期。
同样地,通过将输入日期作为 **'Monday' 和 'Wednesday' 传递给 getPreviousByDay()** 函数来打印上周一和上周三的日期。
示例
以下程序使用 **datetime 模块** 返回确定上周五/任意一天的日期:
# importing datetime, timedelta from datetime module
from datetime import datetime, timedelta
# list of days in a week
weekdaysList = ['Monday', 'Tuesday', 'Wednesday', 'Thursday',
'Friday', 'Saturday', 'Sunday']
# creating a function to return the last weekday of input day by
# accepting the input day, start_date=None as arguments
def getPreviousByDay(inputDay, start_date=None):
# check whether the start date is None
if start_date is None:
# assiging today's date(current date) to the start date
start_date = datetime.today()
# getting the weekday number
dayNumber = start_date.weekday()
# Get the index of the target weekday
dayNumberTarget = weekdaysList.index(inputDay)
# getting the last weekday
daysAgo = (7 + dayNumber - dayNumberTarget) % 7
# checking whether the above days ago result is equal to 0
if daysAgo == 0:
# assigning 7 to the days ago
daysAgo = 7
# Subtract the above number of days from the current date(start date)
# to get the last week's date of the given day
targetDate = start_date - timedelta(days=daysAgo)
# returning the last week's date of the input date
return targetDate
# printing today's/current date and time
print("Current date and time:", datetime.today())
# printing the last Friday date by calling the above defined
# getPreviousByDay() function by passing inputDay as 'Friday' as an argument
print("Last Friday Date:", getPreviousByDay('Friday'))
# printing the last Monday date
print("Last Monday Date:", getPreviousByDay('Monday'))
# printing the last Wednesday date
print("Last Wednesday Date:", getPreviousByDay('Wednesday'))
输出
执行上述程序将生成以下输出:
Current date and time: 2023-01-01 08:15:35.935826 Last Friday Date: 2022-12-30 08:15:35.936020 Last Monday Date: 2022-12-26 08:15:35.936153 Last Wednesday Date: 2022-12-28 08:15:35.936264
方法2:使用 Dateutil 包
在此代码中,**开始日期** 和 **目标日期** 映射到它们在星期中对应的数字位置(以星期一为第 **0** 天)。然后,使用模算术来确定自 **目标日期** 上次出现以来已经过去了多少天。
然后通过从适当的 timedelta 实例中减去开始日期来确定目标日期。
如果您经常运行此类日期计算,则安装 **python-dateutil** 包可能更好。
这是一个使用 dateutil 的 **relativedelta()** 函数进行相同计算的示例。
**dateutil.relativedelta.relativedelta** 支持根据年份、月份、周或天定义的周期,以及为年份、月份或天定义绝对值的能力,而 **timedelta** 仅支持天(和周)。
示例
以下程序使用 **dateutil 模块** 返回确定上周五和下周五的日期:
# importing datetime from datetime module
from datetime import datetime
# importing relativedelta from dateutil module
from dateutil.relativedelta import relativedelta
from dateutil.rrule import *
# taking the start date as the current date
start_date = datetime.now()
# printing the todays/current date and time
print("Current Date:", start_date)
# printing the Next Friday date
print("Next Friday Date:", start_date + relativedelta(weekday=FR))
# printing the Last Friday date
print("Last Friday Date:", start_date + relativedelta(weekday=FR(-1)))
输出
执行上述程序将生成以下输出:
Current Date: 2023-01-15 08:19:49.267666 Next Friday Date: 2023-01-20 08:19:49.267666 Last Friday Date: 2023-01-13 08:19:49.267666
结论
在这篇文章中,我们学习了如何使用两种不同的方法在 Python 中获取上周五的日期。我们还实践了如何获取给定输入日的上一个工作日,以及如何获取给定日的下一个工作日。
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP