转换 ISO 日期

Beginner

This tutorial is from open-source community. Access the source code

简介

在 Python 中,日期可以用多种格式表示。其中一种格式是 ISO-8601 格式,它是表示日期和时间的标准格式。在这个挑战中,你将负责把一个日期从其 ISO-8601 表示形式转换为 datetime.datetime 对象。

转换 ISO 日期

编写一个函数 from_iso_date(d),它接受一个表示 ISO-8601 格式日期的字符串 d,并返回一个表示相同日期和时间的 datetime.datetime 对象。

from datetime import datetime

def from_iso_date(d):
  return datetime.fromisoformat(d)
from_iso_date('2020-10-28T12:30:59.000000') ## 2020-10-28 12:30:59

总结

在这个挑战中,你学习了如何在 Python 中将日期从其 ISO-8601 表示形式转换为 datetime.datetime 对象。这在各种应用程序中处理日期和时间时可能会很有用。