0

The following code gives the error ValueError("time data '2023-03-16T21:30:00.0000000Z' does not match format '%Y-%m-%dT%H:%M:%S.%fZ'") What is wrong?

date_string = '2023-03-16T21:30:00.0000000Z'
day_of_month_in_result = datetime.strptime(date_string, '%Y-%m-%dT%H:%M:%S.%fZ').day
print(day_of_month_in_result)
user1438082
  • 2,740
  • 10
  • 48
  • 82
  • 1
    Does this answer your question? [Parsing datetime strings containing nanoseconds](https://stackoverflow.com/questions/10611328/parsing-datetime-strings-containing-nanoseconds) – Nick ODell Mar 16 '23 at 21:57
  • 2
    From the docs: `%f Microsecond as a decimal number, zero-padded to 6 digits.` This date has 7 digits of precision after the decimal point. If you remove the seventh digit, it parses correctly. – Nick ODell Mar 16 '23 at 21:58
  • @NickODell yes it does. Thank you. See answer below. Spent ages at this ! – user1438082 Mar 16 '23 at 22:06

1 Answers1

0

Use Pandas

import pandas as pd

day_of_month_in_result = pd.to_datetime('2023-03-17T21:30:00.0000000Z', format='%Y-%m-%dT%H:%M:%S.%fZ').date()

user1438082
  • 2,740
  • 10
  • 48
  • 82