简体   繁体   English

Python 带走时间

[英]Python taking away time

I am trying to take away one time to the other to find out how long the users account will be locked for but instead of getting this:我正在尝试一次又一次地找出用户帐户将被锁定多长时间,而不是得到这个:

0:23:48:55

I get this:我明白了:

21 days , 23:48:55

I am using this method:我正在使用这种方法:

end_time = accounts[user].get("today")
start_time = datetime.datetime.now().time().strftime("%d:%H:%M:%S")
total_time =(datetime.datetime.strptime(end_time,'%d:%H:%M:%S') - datetime.datetime.strptime(start_time, '%d:%H:%M:%S'))
print(total_time)

The end_time has tomorrows date in it which is: end_time 中有明天的日期,即:

23:14:29:21 

so taking the current time away should give:所以把当前时间带走应该给:

0:23:48:21 

but not 21 days.但不是 21 天。
Any ideas why this is doing that and how to fix this任何想法为什么这样做以及如何解决这个问题

The problem comes from your start_time because .time() remove the value of the day.问题来自您的 start_time,因为.time()删除了当天的值。

end_time = "23:14:29:21"
start_time = datetime.datetime.now().strftime("%d:%H:%M:%S")
total_time =(datetime.datetime.strptime(end_time,'%d:%H:%M:%S') - datetime.datetime.strptime(start_time, '%d:%H:%M:%S'))
print(total_time)

Would Be More Like This, The .time() Function Removes The Value Of The Day (Instead Of Counting The Number Of Days (Like 0,1))会更像这样, .time() Function 删除当天的值(而不是计算天数(如 0,1))

#Fixed Code
end_time = "23:14:29:21"
start_time = time.strftime("%d:%H:%M:%S")
total_time =(datetime.datetime.strptime(end_time,'%d:%H:%M:%S') - datetime.datetime.strptime(start_time, '%d:%H:%M:%S'))
print(total_time)

changed改变了

start_time = datetime.datetime.now().time().strftime("%d:%H:%M:%S")

to

start_time = time.strftime("%d:%H:%M:%S")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM