如何在 Python 中获取以分钟为单位的时差
How to get time difference in mins in Python
我有一个时间表示形式,格式为字符串:
9:00 am
8:45 pm
例如,12 小时制,无前导小时 0,am/pm 后缀
我需要得到当时和现在之间的时差(以分钟为单位)。
时间总是在未来,所以如果现在是 09:00
,9:10 am
是 10 分钟后,8:50 am
是 23 小时 50 分钟后。
我一直在玩 strptime
但我似乎无法弄清楚如何以兼容的格式获取我的解析时间和 now()
来进行算术运算。
很可能有更适合的模块。但是你可以快速而肮脏地做:
def to_minutes(time_str)
time, suffix = time_str.split(" ")
hours, min = time.split(":")
if suffix == "pm":
hours+=12
min = hours*60+min
return min
time1 = to_minuntes("9:00 am")
time2 = to_minutes("8:45 pm")
diff = time2-time1
if diff < 0:
diff+= 24*60
# and if necessary you can do a
hours = diff //60
minutes = diff % 60
if hours > 0:
final_diff = f"{hours}h {minutes}min"
else:
final_diff = f"{minutes}min"
像这样,确实有 strptime:
from datetime import datetime
timeString1 = "9:00 am"
timeString2 = "8:45 pm"
format = '%I:%M %p'
datetime1 = datetime.strptime(timeString1, format)
datetime2 = datetime.strptime(timeString2, format)
minutes_diff = (datetime2 - datetime1).total_seconds() / 60.0
print(f"the time difference in minutes is: {minutes_diff}")
这是使用 datetime
的变体。
格式字符串FMT = "%I:%M %p"
根据strptime#time.strftime
. and as only datetime
objects support differences, i use those only (using datetime.combine
)选择。
from datetime import datetime, timedelta
FMT = "%I:%M %p"
TODAY_DATE = datetime.today().date()
def to_datetime(t_str):
return datetime.combine(TODAY_DATE, datetime.strptime(t_str, FMT).time())
NOW = to_datetime("9:00 am")
for t_str in ("9:10 am", "8:45 pm"):
t = to_datetime(t_str)
if t < NOW:
t += timedelta(days=1)
print(f"{t_str}, {(t - NOW)}, {(t - NOW).total_seconds() / 60:.0f} min")
它输出:
9:10 am, 0:10:00, 10 min
8:45 pm, 11:45:00, 705 min
我有一个时间表示形式,格式为字符串:
9:00 am
8:45 pm
例如,12 小时制,无前导小时 0,am/pm 后缀
我需要得到当时和现在之间的时差(以分钟为单位)。
时间总是在未来,所以如果现在是 09:00
,9:10 am
是 10 分钟后,8:50 am
是 23 小时 50 分钟后。
我一直在玩 strptime
但我似乎无法弄清楚如何以兼容的格式获取我的解析时间和 now()
来进行算术运算。
很可能有更适合的模块。但是你可以快速而肮脏地做:
def to_minutes(time_str)
time, suffix = time_str.split(" ")
hours, min = time.split(":")
if suffix == "pm":
hours+=12
min = hours*60+min
return min
time1 = to_minuntes("9:00 am")
time2 = to_minutes("8:45 pm")
diff = time2-time1
if diff < 0:
diff+= 24*60
# and if necessary you can do a
hours = diff //60
minutes = diff % 60
if hours > 0:
final_diff = f"{hours}h {minutes}min"
else:
final_diff = f"{minutes}min"
像这样,确实有 strptime:
from datetime import datetime
timeString1 = "9:00 am"
timeString2 = "8:45 pm"
format = '%I:%M %p'
datetime1 = datetime.strptime(timeString1, format)
datetime2 = datetime.strptime(timeString2, format)
minutes_diff = (datetime2 - datetime1).total_seconds() / 60.0
print(f"the time difference in minutes is: {minutes_diff}")
这是使用 datetime
的变体。
格式字符串FMT = "%I:%M %p"
根据strptime#time.strftime
. and as only datetime
objects support differences, i use those only (using datetime.combine
)选择。
from datetime import datetime, timedelta
FMT = "%I:%M %p"
TODAY_DATE = datetime.today().date()
def to_datetime(t_str):
return datetime.combine(TODAY_DATE, datetime.strptime(t_str, FMT).time())
NOW = to_datetime("9:00 am")
for t_str in ("9:10 am", "8:45 pm"):
t = to_datetime(t_str)
if t < NOW:
t += timedelta(days=1)
print(f"{t_str}, {(t - NOW)}, {(t - NOW).total_seconds() / 60:.0f} min")
它输出:
9:10 am, 0:10:00, 10 min
8:45 pm, 11:45:00, 705 min