1

How would I receive an input of a string (time in ISO 8601 format) and convert/calculate the duration from then until now?

Input: 2018-09-29T00:33:33Z

Output: 3 days ago

Thanks.

2 Answers 2

2
from datetime import datetime
import dateutil.parser

a = datetime.now()
#a = datetime.strftime(a, format="%Y-%m-%d%H:%M:%S")
#a = a[:10] + 'T' + a[10:] + 'Z'
a = datetime.strftime(a, format="%Y-%m-%dT%H:%M:%SZ")

a = dateutil.parser.parse(a) - dateutil.parser.parse("2018-09-29T00:33:33Z")
print(str(a))

3 days, 2:00:00
Sign up to request clarification or add additional context in comments.

1 Comment

Why the string concatenation, when you use a format string before? Shouldn't a = datetime.strftime(a, format="%Y-%m-%dT%H:%M:%SZ") work?
0

An easier solution using Arrow.

>>> import arrow
>>> d = arrow.get("2018-09-29T00:33:33Z")
>>> d.humanize()
'3 days ago'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.