Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - Convert string representation of date to ISO 8601

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.

like image 866
Alon Gubkin Avatar asked Dec 16 '10 12:12

Alon Gubkin


People also ask

What is ISO date format Python?

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.

Is ISO 8601 a string?

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 .


1 Answers

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 
like image 161
unutbu Avatar answered Oct 03 '22 04:10

unutbu