以下函数将为每个星期四打印 It is Friday 或为任何给定日期打印下一个 Thursday 的日期。
import datetime
def next_thursday(date):
# Thursday is day 3 in Python (Monday = 0)
days_ahead = 3 - date.weekday()
if days_ahead == 0:
print("It is Thursday")
# if Thursday has already happened this week we need to add 7 days to the difference
elif days_ahead < 0:
days_ahead += 7
# actually add the time delta using timedelta()
print(date + datetime.timedelta(days=days_ahead), "will be the next Thursday")
date = datetime.datetime.today()
next_thursday(date)
The following function will print It is Thursday for every Thursday or print the date of the next Thursday for any given date.
import datetime
def next_thursday(date):
# Thursday is day 3 in Python (Monday = 0)
days_ahead = 3 - date.weekday()
if days_ahead == 0:
print("It is Thursday")
# if Thursday has already happened this week we need to add 7 days to the difference
elif days_ahead < 0:
days_ahead += 7
# actually add the time delta using timedelta()
print(date + datetime.timedelta(days=days_ahead), "will be the next Thursday")
date = datetime.datetime.today()
next_thursday(date)
The following is a Python script that will apply abovementioned function to 20 generated random dates in a dataframe column.
发布评论
评论(1)
以下函数将为每个星期四打印
It is Friday
或为任何给定日期打印下一个Thursday
的日期。以下是一个 Python 脚本,它将上述函数应用于数据帧列中 20 个生成的随机日期。
The following function will print
It is Thursday
for every Thursday or print the date of the nextThursday
for any given date.The following is a Python script that will apply abovementioned function to 20 generated random dates in a dataframe column.