PythonDatetime.date类的fromisoformat()函数
fromisoformat()函数用于从包含 ISO 格式日期的指定字符串构造日期对象。即,yyyy-mm-dd。
Syntax: @classmethod fromisoformat(date_string)
Parameters: This function accepts a parameter which is illustrated below:
- date_string: This is the specified ISO format i.e., yyyy-mm-dd date string whose date object is going to be constructed.
Return values: This function returns a date object constructed from the specified ISO format date string.
示例 1:从包含 ISO 格式日期的指定字符串中获取日期对象。即,yyyy-mm-dd
Python3
# Python3.7 code for Getting
# a date object from a specified
# string that contains date in
# ISO format. i.e., yyyy-mm-dd
# Importing datetime module
import datetime
# Initializing a date
Date = "2012-10-12";
# Calling fromisoformat() function to
# construct a datetime.date object
New_date = datetime.date.fromisoformat(Date);
# Printing the new constructed date object
print("The constructed date object is: %s"%New_date);
Python3
# Python3.7 code for Getting
# a date object from a specified
# string that contains date in
# ISO format. i.e., yyyy-mm-dd
# Importing datetime module
from datetime import date
# Calling the fromisoformat() function
# to Print the new created date object
# for the specified date in
# ISO format of 2020-10-09
print(date.fromisoformat('2020-10-09'))
输出:
The constructed date object is: 2012-10-12
示例2:获取指定日期新创建的日期对象。
蟒蛇3
# Python3.7 code for Getting
# a date object from a specified
# string that contains date in
# ISO format. i.e., yyyy-mm-dd
# Importing datetime module
from datetime import date
# Calling the fromisoformat() function
# to Print the new created date object
# for the specified date in
# ISO format of 2020-10-09
print(date.fromisoformat('2020-10-09'))
输出:
2020-10-09