0

In my django project i have to convert a str variable passed as a date ("2021-11-10") to a datetime with timezone object for execute an ORM filter on a DateTime field. In my db values are stored as for example:

2021-11-11 01:18:04.200149+00

i try:

# test date
df = "2021-11-11"
df = df + " 00:00:00+00"
start_d = datetime.strptime(df, '%Y-%m-%d %H:%M:%S%Z')

but i get an error due to an error about str format and datetime representation (are different)

How can i convert a single date string into a datetimeobject with timezone stated from midnight of the date value?

So many thanks in advance

Manuel Santi
  • 1,106
  • 17
  • 46

1 Answers1

0

It's not the way to datetime.strptime. Read a little bit more here I believe it will help you. you should implement month as str and without "-".

good luck

Bernana
  • 245
  • 1
  • 12
  • `strptime` won't work here, even with a "correct" parsing directive (try `datetime.strptime("2021-11-11 01:18:04.200149+00", "%Y-%m-%d %H:%M:%S.%f%z")`). OP needs to prepend `':00'` to make this work or use a third party lib parser, e.g. `dateutil.parser.parse("2021-11-11 01:18:04.200149+00")`. – FObersteiner Nov 19 '21 at 08:20