0

I am trying to convert this complicated datetime format after getting this error:

[u"'Nov 11, 2017 5:19:38 AM PST' value has an invalid format. It must be in YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ] format."]

I am building this function but I am not sure how to handle the Month as a word. This is an example of the date as it is in the CSV file: Nov 1, 2017 12:00:13 AM PDT

I am not having any luck:

def process_datetime(self,dt):
    d_time = datetime.strptime(dt, '')
    return datetime.strftime(d_time, 'YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ]')
  • Try `%b` for the abbreviated month. See here: https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior – pault Dec 26 '17 at 16:19
  • (dt, '%b %d, %Y %-I:%M:%S %p %Z') –  Dec 26 '17 at 16:22

1 Answers1

1

try below code, credit goes to this answer (https://stackoverflow.com/a/26435566/6039974)

from datetime import datetime, timedelta
from email.utils import parsedate_tz, mktime_tz

given_date = "Nov 1, 2017 12:00:13 AM PDT";

timestamp = mktime_tz(parsedate_tz(given_date))
utc_dt = datetime(1970, 1, 1) + timedelta(seconds=timestamp)
print(utc_dt)

prints

2017-11-01 12:00:13
johnII
  • 1,423
  • 1
  • 14
  • 20