Convert 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.
使用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
Python inbuilt datetime package has build in method to convert a datetime object to isoformat. Here is a example:
>>>from datetime import datetime
>>>date = datetime.strptime('Thu, 16 Dec 2010 12:14:05', '%a, %d %b %Y %H:%M:%S')
>>>date.isoformat()
output is
'2010-12-16T12:14:05'
链接地址: http://www.djcxy.com/p/35112.html
上一篇: xCBL的ISO 8601日期时间是否包含破折号?
下一篇: 将日期转换为ISO 8601