如何从时间字符串中减去datenow?

时间:2022-07-28 16:30:13

I have a problem that seems really easy but I can't figure it out.

我有一个看似非常容易的问题,但我无法弄清楚。

I want to achieve the following: Time_as_string - time_now = minutes left until time as string.

我想实现以下内容:Time_as_string - time_now =分钟,直到时间为字符串。

I scrape a time from a website as a string, for example: '15:30'.

我从网站上抽出时间作为字符串,例如:'15:30'。

I want to subtract the current time from this to show how many minutes are left untill the scraped time string.

我想从中减去当前时间,以显示剩下多少分钟,直到刮掉的时间字符串。

I tried many things like strftime(), converting to unix timestamp, googling solutions etc. I can make a time object from the string through strftime() but I can't subtract it from the current time.

我尝试了很多东西,比如strftime(),转换为unix时间戳,google搜索解决方案等。我可以通过strftime()从字符串中创建一个时间对象,但我不能从当前时间中减去它。

What is the best way to achieve this?

实现这一目标的最佳方法是什么?

3 个解决方案

#1


0  

If '15:30' belongs to today:

如果'15:30'属于今天:

#!/usr/bin/env python3
from datetime import datetime, timedelta

now = datetime.now()
then = datetime.combine(now, datetime.strptime('15:30', '%H:%M').time())
minutes = (then - now) // timedelta(minutes=1)

If there could be midnight between now and then i.e., if then is tomorrow; you could consider a negative difference (if then appears to be in the past relative to now) to be an indicator of that:

如果从现在到现在可能有午夜,即如果那是明天;你可以考虑一个负面的差异(如果那时似乎过去相对于现在)作为一个指标:

while then < now:
    then += timedelta(days=1)
minutes = (then - now) // timedelta(minutes=1)

On older Python version, (then - now) // timedelta(minutes=1) doesn't work and you could use (then - now).total_seconds() // 60 instead.

在较旧的Python版本上,(然后 - 现在)// timedelta(分钟= 1)不起作用,您可以使用(然后 - 现在).total_seconds()// 60代替。

The code assumes that the utc offset for the local timezone is the same now and then. See more details on how to find the difference in the presence of different utc offsets in this answer.

该代码假定本地时区的utc偏移量时不时相同。有关如何在此答案中找到存在不同utc偏移的差异的详细信息。

#2


1  

from datetime import datetime

s = "15:30"
t1 = datetime.strptime(s,"%H:%M")

diff = t1 - datetime.strptime(datetime.now().strftime("%H:%M"),"%H:%M")

print(diff.total_seconds() / 60)
94.0

#3


-1  

The easiest way is probably to subtract two datetimes from each other and use total_seconds():

最简单的方法可能是相互减去两个日期时间并使用total_seconds():

>>> d1 = datetime.datetime(2000, 1, 1, 20, 00)
>>> d2 = datetime.datetime(2000, 1, 1, 16, 30)
>>> (d1 - d2).total_seconds()
12600.0

Note that this won't work if the times are in different timezones (I just picked January 1, 2000 to make it a datetime). Otherwise, construct two datetimes in the same timezones (or UTC), subtract those and use total_seconds() again to get the difference (time left) in seconds.

请注意,如果时间位于不同的时区(我刚刚选择2000年1月1日使其成为日期时间),这将不起作用。否则,在相同的时区(或UTC)中构造两个日期时间,减去它们并再次使用total_seconds()来获得差异(剩余时间),以秒为单位。

#1


0  

If '15:30' belongs to today:

如果'15:30'属于今天:

#!/usr/bin/env python3
from datetime import datetime, timedelta

now = datetime.now()
then = datetime.combine(now, datetime.strptime('15:30', '%H:%M').time())
minutes = (then - now) // timedelta(minutes=1)

If there could be midnight between now and then i.e., if then is tomorrow; you could consider a negative difference (if then appears to be in the past relative to now) to be an indicator of that:

如果从现在到现在可能有午夜,即如果那是明天;你可以考虑一个负面的差异(如果那时似乎过去相对于现在)作为一个指标:

while then < now:
    then += timedelta(days=1)
minutes = (then - now) // timedelta(minutes=1)

On older Python version, (then - now) // timedelta(minutes=1) doesn't work and you could use (then - now).total_seconds() // 60 instead.

在较旧的Python版本上,(然后 - 现在)// timedelta(分钟= 1)不起作用,您可以使用(然后 - 现在).total_seconds()// 60代替。

The code assumes that the utc offset for the local timezone is the same now and then. See more details on how to find the difference in the presence of different utc offsets in this answer.

该代码假定本地时区的utc偏移量时不时相同。有关如何在此答案中找到存在不同utc偏移的差异的详细信息。

#2


1  

from datetime import datetime

s = "15:30"
t1 = datetime.strptime(s,"%H:%M")

diff = t1 - datetime.strptime(datetime.now().strftime("%H:%M"),"%H:%M")

print(diff.total_seconds() / 60)
94.0

#3


-1  

The easiest way is probably to subtract two datetimes from each other and use total_seconds():

最简单的方法可能是相互减去两个日期时间并使用total_seconds():

>>> d1 = datetime.datetime(2000, 1, 1, 20, 00)
>>> d2 = datetime.datetime(2000, 1, 1, 16, 30)
>>> (d1 - d2).total_seconds()
12600.0

Note that this won't work if the times are in different timezones (I just picked January 1, 2000 to make it a datetime). Otherwise, construct two datetimes in the same timezones (or UTC), subtract those and use total_seconds() again to get the difference (time left) in seconds.

请注意,如果时间位于不同的时区(我刚刚选择2000年1月1日使其成为日期时间),这将不起作用。否则,在相同的时区(或UTC)中构造两个日期时间,减去它们并再次使用total_seconds()来获得差异(剩余时间),以秒为单位。