0

I have this string:

date_string = "2022-04-11 00:00:00.000000"

which I'm trying to convert to this format:

date = "2022-04-11T00:00:00.000Z"

Can anyone please help me with the same

i tried to do this but am getting Error like invalid format

datetime.strptime(date_string, "%Y-%m-%dT %H:%M:%S%z")
Newton8989
  • 300
  • 1
  • 4
  • 22

1 Answers1

2

There's a library known as dateutil. With the help of that, we parse the string into date format and then we convert it into the isoformat.

import dateutil.parser as parser

date_string = "2022-04-11 00:00:00.000000"

date = parser.parse(date_string)

print(date.strftime('%Y-%m-%dT%H:%M:%SZ'))

The output would be 2022-04-11T00:00:00Z