We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- Python
- Date and Time
- Time Delta
- Discussions
Time Delta
Time Delta
Sort by
recency
|
380 Discussions
|
Please Login in order to post a comment
date_format = "%a %d %b %Y %H:%M:%S %z"
why is the above giving me error?
why
my code!!
from datetime import datetime
def time_delta(t1, t2): format_str = "%a %d %b %Y %H:%M:%S %z" datetime1 = datetime.strptime(t1, format_str) datetime2 = datetime.strptime(t2, format_str) delta = abs((datetime1 - datetime2).total_seconds()) return str(int(delta))
if name == "main": t = int(input()) for _ in range(t): t1 = input().strip() t2 = input().strip() print(time_delta(t1, t2))
It's a great exercise to reinforce working with date-time formats and conversions. Calculating the absolute difference in seconds requires careful handling of the time zone offsets. Mahadev bet777