0

I'm using Python 3.7 and none of the solutions from this post -- Python strptime() and timezones? applied to me. I had this for generating a datetime from text ...

datetime.strptime(created_on_txt, '%Y-%m-%dT%H:%M:%S%z')

This later results in the warning ...

DateTimeField Article.created_on received a naive datetime (2019-12-09 00:08:17.670597) while time zone support is active.

So I added a timezone like so ...

datetime(pytz.timezone(settings.TIME_ZONE)).strptime(created_on_txt, '%Y-%m-%dT%H:%M:%S%z')

But this results in the error ...

TypeError: an integer is required

What's the right way to get the timezone into the datetime?

Dave
  • 15,639
  • 133
  • 442
  • 830
  • if you don't mind using pandas, its to_datetime method is pretty flexible `pd.to_datetime('2017-01-01T01:01:01 EST')` – Marat Dec 09 '19 at 16:18
  • Does this answer your question? [pandas.plot argument c vs s](https://stackoverflow.com/questions/52412449/pandas-plot-argument-c-vs-s) – abhilb Dec 09 '19 at 16:39
  • @Marat, if I already have the date as a string (e.g. "2017-01-01T01:01:01"), without the timezone, how do I create a datetime object given that I know the timezone as a separate string (e.g. America/New York)? – Dave Dec 09 '19 at 16:47
  • @Dave just append the timezone (EST for NY) to the end of the string: `pd.to_datetime('2017-01-01T01:01:01' + ' EST)` – Marat Dec 09 '19 at 17:29

0 Answers0