如何在 Python 中生成未来日期时间序列并从集合中确定最近的日期时间
我需要在 Python 中生成四个日期时间对象:
"The next instance of 5:30AM EST"
"The next instance of 8:30AM EST"
"The next instance of 1:00PM EST"
"The next instance of 5:30PM EST"
然后我需要找到其中哪个最接近当前日期/时间。
我希望我能说我有一些起始代码,但我不知道从哪里开始。
I need to generate four datetime objects in Python:
"The next instance of 5:30AM EST"
"The next instance of 8:30AM EST"
"The next instance of 1:00PM EST"
"The next instance of 5:30PM EST"
Then I need to find which of those is closest to the current date/time.
I wish I could say I have some starting code, but I have no idea where to start on this one.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这应该可以帮助您开始。我将当前时间作为日期时间传递到函数中,因此如果参数采用 EST,这应该可以工作。
结果将是按顺序排列的对象,从“最快”的对象开始,依此类推。
This should get you started. I have the current time being passed into the function as a datetime, so if the argument is in EST, this should just work.
The result will be the objects, in order, starting with the "soonest" one, and so on.
这看起来可能是一个家庭作业问题。这些示例代码足以帮助您入门吗?它可能不是最有效的,但它会起作用。
对于时间列表“t”,您可以使用:
t = [时间(5,30), 时间(8,30), 时间(13,0), 时间(17,30)]
now = 日期时间.now()
This seems like it might be a homework question. Is this enough sample code to get you started? It's probably not the MOST efficient, but it'll work.
For a list of times, "t", you can use:
t = [time(5,30), time(8,30), time(13,0), time(17,30)]
now = datetime.now()