Newton8989
Newton8989

Reputation: 362

python get isoformat date time from time

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")

Upvotes: 0

Views: 624

Answers (1)

Mohit Bagadiya
Mohit Bagadiya

Reputation: 21

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

Upvotes: 2

Related Questions