Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (17.6k points)

I have a JSON date data set and trying to calculate the time difference between two different JSON DateTime.

For example :

'2015-01-28T21:41:38.508275' - '2015-01-28T21:41:34.921589'

Please look at the python code below:

#let's say 'time' is my data frame and JSON formatted time values are under the 'due_date' column

time_spent = time.iloc[2]['due_date'] - time.iloc[10]['due_date']

This doesn't work. I also tried to cast each operand to int, but it also didn't help. What are the different ways to perform this calculation?

1 Answer

0 votes
by (41.4k points)

Use parser from dateutil.

Code:

from dateutil.parser import parse

first_date_obj = parse("2015-01-28T21:41:38.508275")

second_date_obj = parse("2015-02-28T21:41:38.508275")

print(second_date_obj - first_date_obj)

Related questions

0 votes
1 answer
asked Jul 19, 2019 in BI by Vaibhav Ameta (17.6k points)
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Dec 10, 2020 in Python by laddulakshana (16.4k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...