# ISO8601

# ISO 8601类

国际标准ISO 8601,是国际标准化组织的日期和时间的表示方法,全称为《数据存储和交换形式·信息交换·日期和时间的表示方法》,在API接口开发中涉及的比较多。

>>> import dateutil.parser
>>> dateutil.parser.parse('2008-09-03T20:56:35.450686Z') # RFC 3339 format
datetime.datetime(2008, 9, 3, 20, 56, 35, 450686, tzinfo=tzutc())
>>> dateutil.parser.parse('2008-09-03T20:56:35.450686') # ISO 8601 extended format
datetime.datetime(2008, 9, 3, 20, 56, 35, 450686)
>>> dateutil.parser.parse('20080903T205635.450686') # ISO 8601 basic format
datetime.datetime(2008, 9, 3, 20, 56, 35, 450686)
>>> dateutil.parser.parse('20080903') # ISO 8601 basic format, date only
datetime.datetime(2008, 9, 3, 0, 0)

或者使用如下方式解析:

>>> datetime.datetime.strptime("2008-09-03T20:56:35.450686Z", "%Y-%m-%dT%H:%M:%S.%fZ")

另外还可以使用iso8601模块:http://pyiso8601.readthedocs.io/en/latest/

其他日期与时间工具:

  • 公历转农历:https://pypi.python.org/pypi/LunarSolarConverter/
  • 口语化日期:https://github.com/scrapinghub/dateparser
  • Moment:https://github.com/zachwill/moment
  • Delorean:https://github.com/myusuf3/delorean
  • When:https://whenpy.readthedocs.io/en/latest/
  • Pendulum:https://pendulum.eustace.io/
  • 时间机器:https://github.com/spulec/freezegun
  • 工作日历:https://github.com/peopledoc/workalendar
  • 中国法定节假日:https://github.com/NateScarlet/holiday-cn