0

I have this date format (thousands in this form).

time_from = '2020-11-28T06:00:00-06:00'
time_to = '2020-11-28T09:00:00-06:00'

And I want it to transform them into datetime format so I can compute the total # of seconds between the times quickly (see below). I'm thinking a for loop where I go through each hour/day of the string and save them in a list. But, wondering if there's an easier way?

import datetime as dt

a = dt.datetime(2013,12,30,23,59,59)
b = dt.datetime(2013,12,31,23,59,59)

(b-a).total_seconds()
carsof
  • 83
  • 1
  • 8
  • I didn't understand what difficulty you were facing. Have you looked up how to parse this date format using `strptime` [here](https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes)? – mkrieger1 Jun 04 '21 at 19:28
  • do you have those 1000s of time_from/time_to in a dataframe? – Jayvee Jun 04 '21 at 19:28
  • Does this answer your question? [How do I parse an ISO 8601-formatted date?](https://stackoverflow.com/questions/127803/how-do-i-parse-an-iso-8601-formatted-date) – FObersteiner Jun 04 '21 at 20:20

1 Answers1

3

You can use fromisoformat to do this. Refer this.

import datetime

start = datetime.datetime.fromisoformat('2020-11-28T06:00:00-06:00')
end = datetime.datetime.fromisoformat('2020-11-28T09:00:00-06:00')
(end-start).total_seconds()
Nishan
  • 3,644
  • 1
  • 32
  • 41