下周五如何计算? [英] How to calculate next Friday?
问题描述
如何计算下一个星期五的日期?
How can I calculate the date of the next Friday?
推荐答案
首先,您需要 datetime
库:
import datetime
然后您需要一个开始日期;就是今天。
Then you need a starting date; that is, today.
d = datetime.date.today()
从那里开始,您要一直前进直到到达星期五。 date.weekday
方法将星期一至星期日表示为0到6,因此:
Starting from there, you'll want to keep going forward until you reach Friday. The date.weekday
method represents Monday through Sunday as 0 through 6, so:
while d.weekday() != 4:
如果当前日期不是星期五,则必须一次添加一天。要将时间间隔添加到 date
对象,请使用 timedelta
对象。
If the current day isn't Friday, you'll have to add a day, one at a time. To add an interval of time to a date
object, you use a timedelta
object.
d += datetime.timedelta(1)
将它们放在一起,然后 d
最终将包含一个表示下星期五的 date
对象。注意,如果今天是星期五,则此代码将在今天产生;如果需要下周五进行生产,则可以对其进行调整。
Put it all together, and d
will ultimately contain a date
object representing next Friday. Note that if today is Friday, this code will produce today; you can tweak it if you need it to produce next Friday instead.
这篇关于下周五如何计算?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!