📌  相关文章
📜  在Python中将“未知格式”字符串转换为日期时间对象

📅  最后修改于: 2022-05-13 01:54:38.089000             🧑  作者: Mango

在Python中将“未知格式”字符串转换为日期时间对象

在本文中,我们将看到如何在Python中将“未知格式”字符串转换为 DateTime 对象。

假设有两个字符串包含未知格式的日期和我们不知道的格式。我们所知道的是,两个字符串都包含有效的日期时间表达式。通过使用包含日期解析器的 dateutil 模块,该模块可以解析多种格式的日期字符串。

让我们看一些示例,以多种方式说明这种转换:

示例 1:将“未知格式”字符串转换为日期时间对象

在下面的示例中,日期字符串“19750503T080120”的指定未知格式被解析为 DateTime 对象的有效已知格式。

Python3
# Python3 code to illustrate the conversion of
# "unknown format" strings to DateTime objects
  
# Importing parser from the dateutil.parser
import dateutil.parser as parser
  
# Initializing an unknown format date string
date_string = "19750503T080120"
  
# Calling the parser to parse the above
# specified unformatted date string
# into a datetime objects
date_time = parser.parse(date_string)
  
# Printing the converted datetime object
print(date_time)


Python3
# Python3 code to illustrate the conversion of
# "unknown format" strings to DateTime objects
  
# Importing parser from the dateutil.parser and
# dt from datetime module
import dateutil.parser as parser
import datetime as dt
  
# Calling the now() function to
# return the current datetime
now = dt.datetime.now()
  
# Calling the isoformat() to return the
# current datetime in isoformat and print it
print(now.isoformat())
  
# Now calling the parser to parse the
# above returned isoformat datetime into
# a valid known format of datetime object
print(parser.parse(now.isoformat()))


Python3
# Python3 code to illustrate the conversion of
# "unknown format" strings to DateTime objects
  
# Importing parser from the dateutil.parser
import dateutil.parser as parser
  
# Now parsing the "10-09-2021" datetime with
# dayfirst parameter
B = parser.parse("10-09-2021", dayfirst = True)
  
# Printing the parsed datetime
print(B)


输出:



1975-05-03 08:01:20

示例 2:将当前日期和时间转换为 datetime 对象

在下面的示例中,当前日期和时间已被解析为 datetime 对象。

蟒蛇3

# Python3 code to illustrate the conversion of
# "unknown format" strings to DateTime objects
  
# Importing parser from the dateutil.parser and
# dt from datetime module
import dateutil.parser as parser
import datetime as dt
  
# Calling the now() function to
# return the current datetime
now = dt.datetime.now()
  
# Calling the isoformat() to return the
# current datetime in isoformat and print it
print(now.isoformat())
  
# Now calling the parser to parse the
# above returned isoformat datetime into
# a valid known format of datetime object
print(parser.parse(now.isoformat()))

输出:

2021-08-20T13:35:23.829488
2021-08-20 13:35:23.829488

有时 DateTime字符串可能不明确,例如 01-02-2003 可能意味着 1 月 2 日或 2 月 1 日。为了消除这种歧义,我们有两个参数,例如 dayfirst 和 yearfirst,如下例所示。

示例 3:将特定未知格式转换为日期时间对象

在下面的例子中,指定的日期时间是“10-09-2021”。 parse() 使用参数 dayfirst 作为 True 并返回输出为“2021-09-10 00:00:00”,即 2021 年的 10 月 9 日。

蟒蛇3

# Python3 code to illustrate the conversion of
# "unknown format" strings to DateTime objects
  
# Importing parser from the dateutil.parser
import dateutil.parser as parser
  
# Now parsing the "10-09-2021" datetime with
# dayfirst parameter
B = parser.parse("10-09-2021", dayfirst = True)
  
# Printing the parsed datetime
print(B)

输出:

2021-09-10 00:00:00