0

I have a dictionary of strings that are formatted in a UTC ISO 8601 manner.

Example: "2022-11-05T22:36:48.701614+01:00"

I want to be able to convert these into seconds so that I can calculate differences and such. I see there is the "datetime" python library that seems well equipped to handle this. But, I can't quite get it to work.

Thanks

I tried the datetime.timezone.fromutc() function but it seems to expect the string already formatted.

Kyle
  • 23
  • 4
  • What does the `dict` look like `{'Date': ["2022-11-05T22:36:48.701614+01:00", "2022-12-05T22:36:48.701614+01:00", ...]}`? Or do you have a `set`: `{"2022-11-05T22:36:48.701614+01:00", "2022-12-05T22:36:48.701614+01:00", ...}` – It_is_Chris Mar 14 '23 at 16:07
  • "*convert these into seconds so that I can calculate differences and such*" - you do not need to convert to seconds, you have [timedelta](https://docs.python.org/3/library/datetime.html#timedelta-objects) to handle durations. See [How do I parse an ISO 8601-formatted date?](https://stackoverflow.com/q/127803/10197418) on how to parse the string to datetime. – FObersteiner Mar 14 '23 at 16:18
  • Thanks for the recommendation of the linked answer. I don't know why I couldn't find it. https://stackoverflow.com/questions/127803/how-do-i-parse-an-iso-8601-formatted-date **dateutil.parser.isoparse('2008-09-03T20:56:35.450686') # ISO 8601 extended format ** **datetime.datetime(2008, 9, 3, 20, 56, 35, 450686)** These objects allowed me to do the desired operations. – Kyle Mar 15 '23 at 10:48

0 Answers0