如何计算下周五凌晨3点?
如何将下周五凌晨 3 点计算为 datetime
对象?
说明:即,计算出的日期应始终大于 7 天且小于或等于 14 天。
How can you calculate the following Friday at 3am as a datetime
object?
Clarification: i.e., the calculated date should always be greater than 7 days away, and less than or equal to 14.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
根据您的澄清...我认为您可以执行以下操作:
注意
isoweekday()
从周一到周日返回 1 到 7。 12 代表下周的星期五。因此 12 - Today.isoweekday() 为您提供了需要添加到今天的正确时间增量。希望这有帮助。
Based on your clarification... I think you can do something like this:
Notice
isoweekday()
returns 1 to 7 for monday to sunday. 12 represents friday of the following week. So 12 - today.isoweekday() gives you the correct time delta you need to add to today.Hope this helps.
使用 pendulum,您可以执行以下操作:
请注意,此行中有两个
next Friday
。要将其转换为字符串,
With pendulum, you can do:
Note that there are two
next Friday
in this line.To convert it into string,
如果您安装 dateutil,那么您可以执行以下操作:
上面,
hours=+21< /code> 告诉
relativedelta
在查找下周五之前将dt
增加 21 小时。因此,如果dt
是 2010 年 3 月 12 日凌晨 2 点,添加 21 小时将使其成为当天的晚上 11 点,但如果dt
是在凌晨 3 点之后,然后添加 21 小时将dt
推入星期六。这是一些测试代码。
其产量:
而在凌晨 3 点之前:
产量:
If you install dateutil, then you could do something like this:
Above,
hours=+21
tellsrelativedelta
to increment thedt
by 21 hours before finding the next Friday. So, ifdt
is March 12, 2010 at 2am, adding 21 hours makes it 11pm of the same day, but ifdt
is after 3am, then adding 21 hours pushsdt
into Saturday.Here is some test code.
which yields:
while before 3am:
yields:
这是一个满足OP要求的函数和测试:
Here's a function and a test that it meets the OP's requirements:
一般来说,我喜欢 dateutil 来完成此类任务,但我不明白你想要的启发式方法 - 作为我使用这样的词,如果我说“下周五”,而今天是周四,我的意思是明天(可能是我工作太辛苦,忘记了今天是星期几)。当然,如果您可以严格指定您的启发式方法,那么它们肯定可以被编程,但是如果它们足够奇怪,您就不太可能发现它们已经在现有包中为您预先编程了;-)。
I like dateutil for such tasks in general, but I don't understand the heuristics you want -- as I use the words, if I say "next Friday" and it's Thursday I would mean tomorrow (probably I've been working too hard and lost track of what day of the week it is). If you can specify your heuristics rigorously they can surely be programmed, of course, but if they're weird and quirky enough you're unlikely to find them already pre-programmed for you in existing packages;-).