In Python, how can I convert a string like this:
Thu, 16 Dec 2010 12:14:05 +0000
to ISO 8601 format, while keeping the timezone?
Please note that the orginal date is string, and the output should be string too, not datetime
or something like that.
I have no problem to use third parties libraries, though.
Format of ISO 8601 Date: In Python ISO 8601 date is represented in YYYY-MM-DDTHH:MM:SS. mmmmmm format. For example, May 18, 2022, is represented as 2022-05-18T11:40:22.519222.
The toISOString() method returns a string in simplified extended ISO format (ISO 8601), which is always 24 or 27 characters long ( YYYY-MM-DDTHH:mm:ss. sssZ or ±YYYYYY-MM-DDTHH:mm:ss. sssZ , respectively). The timezone is always zero UTC offset, as denoted by the suffix Z .
Using dateutil:
import dateutil.parser as parser text = 'Thu, 16 Dec 2010 12:14:05 +0000' date = parser.parse(text) print(date.isoformat()) # 2010-12-16T12:14:05+00:00
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With