0

I aim to convert

stringtime = '2020-02-30 10:27:00+01:00'

so that I can compare it to

nowtime = datetime.datetime.utcnow()

using

if nowtime > stringtime:
    print(1)

I tried strptime:

datetime.datetime.strptime(stringtime, '%Y-%m-%d %H:%M:%S')

But cannot find a format specification for the timezone in the strptime documentation.

I also tried

pandas.Timestamp(stringtime)

but I get ValueError: could not convert string to Timestamp.

How can this be done?

cheesus
  • 1,111
  • 1
  • 16
  • 44
  • Does this answer your question? [Python strptime() and timezones?](https://stackoverflow.com/questions/3305413/python-strptime-and-timezones) – Błotosmętek Feb 27 '20 at 10:33

2 Answers2

2
datetime.datetime.strptime(stringtime, '%Y-%m-%d %H:%M:%S%z')

Will give you the expected result %z is the format (Python3 only), however your original date is invalid as February doesnt have 30 days :)

tomgalpin
  • 1,943
  • 1
  • 4
  • 12
1

First of all: Your stringtime is wrong, there exists no February 30th. ;)

You can achieve what you want with dateutil:

import dateutil.parser
stringtime = '2020-03-30 10:27:00+01:00'
dateutil.parser.isoparse(stringtime)
# datetime.datetime(2020, 3, 30, 10, 27, tzinfo=tzoffset(None, 3600))
Mat-KH
  • 76
  • 4