从字符串创建Python日期时间
在本文中,我们将看到如何从给定的字符串创建一个Python DateTime 对象。
为此,我们将使用datetime.strptime()方法。 strptime() 方法返回一个 date_string 对应的 DateTime 对象,根据用户给出的格式字符串进行解析。
Syntax: datetime.strptime(date_string, format)
使用 strptime() 将字符串转换为日期时间
在这里,我们将一个简单的字符串转换为 datetime 对象,为此我们将字符串传递给 strptime() 并通过 this 对象化 datetime 对象。
Python3
from datetime import datetime
input_str = '21/01/24 11:04:19'
dt_object = datetime.strptime(
input_str, '%d/%m/%y %H:%M:%S')
print("The type of the input date string now is: ",
type(dt_object))
print("The date is", dt_object)
Python3
from datetime import datetime
time_str = 'May 17 2019 11:33PM'
dt_object = datetime.strptime(
time_str, '%b %d %Y %I:%M%p')
print(dt_object)
Python3
from datetime import datetime
time_str = '201123101455'
dt_obj = datetime.strptime(time_str, '%y%m%d%H%M%S')
dt_obj2 = datetime.strptime(time_str, '%d%m%y%H%S%M')
print ("1st interpretation of date from string is: ",dt_obj)
print ("2nd interpretation of date from same string is", dt_obj2)
Python3
from datetime import datetime
time_str = '220917 114519'
dt_obj = datetime.strptime(time_str, '%d/%m/%y %H:%M:%S')
print ("The type is", type(dt_obj))
print ("The date is", date_time_obj)
输出:
The type of the input date string now is:
The date is 2024-01-21 11:04:19
使用 strptime() 将包含单词的字符串转换为日期时间
strptime() 方法也允许您将“单词”中的时间戳转换为日期时间对象。下面的片段显示它可以完成:
蟒蛇3
from datetime import datetime
time_str = 'May 17 2019 11:33PM'
dt_object = datetime.strptime(
time_str, '%b %d %Y %I:%M%p')
print(dt_object)
输出:
2019-05-17 23:33:00
Python strptime() 值错误
必须知道给定字符串的DateTime 格式,否则会导致不必要的问题和错误。下面的片段显示了可能导致的问题:
蟒蛇3
from datetime import datetime
time_str = '201123101455'
dt_obj = datetime.strptime(time_str, '%y%m%d%H%M%S')
dt_obj2 = datetime.strptime(time_str, '%d%m%y%H%S%M')
print ("1st interpretation of date from string is: ",dt_obj)
print ("2nd interpretation of date from same string is", dt_obj2)
输出 :
1st interpretation of date from string is: 2020-11-23 10:14:55
2nd interpretation of date from same string is 2023-11-20 10:55:14
如果字符串参数与格式参数不一致,则 strptime() 方法将不起作用。以下片段显示了由于格式说明符不匹配而发生的错误。
蟒蛇3
from datetime import datetime
time_str = '220917 114519'
dt_obj = datetime.strptime(time_str, '%d/%m/%y %H:%M:%S')
print ("The type is", type(dt_obj))
print ("The date is", date_time_obj)
输出:
ValueError(“time data %r does not match format %r” %(data_string, format))
格式代码列表
格式说明符大部分与 strftime() 方法相同。这些说明符是: Directives Meaning Example %a Abbreviated weekday name. Sun, Mon, … %A Full weekday name. Sunday, Monday, … %w Weekday as a decimal number. 0, 1, …, 6 %d Day of the month as a zero-padded decimal. 01, 02, …, 31 %-d Day of the month as a decimal number. 1, 2, …, 30 %b Abbreviated month name. Jan, Feb, …, Dec %B Full month name. January, February, … %m Month as a zero-padded decimal number. 01, 02, …, 12 %-m Month as a decimal number. 1, 2, …, 12 %y Year without century as a zero-padded decimal number. 00, 01, …, 99 %-y Year without century as a decimal number. 0, 1, …, 99 %y Year with century as a decimal number. 2013, 2019 etc. %H Hour (24-hour clock) as a zero-padded decimal number. 00, 01, …, 23 %-H Hour (24-hour clock) as a decimal number. 0, 1, …, 23 %I Hour (12-hour clock) as a zero-padded decimal number. 01, 02, …, 12 %-I Hour (12-hour clock) as a decimal number. 1, 2, … 12 %p Locale’s AM or PM. AM, PM %M Minute as a zero-padded decimal number. 00, 01, …, 59 %-M Minute as a decimal number. 0, 1, …, 59 %S Second as a zero-padded decimal number. 00, 01, …, 59 %-S Second as a decimal number. 0, 1, …, 59 %f Microsecond as a decimal number, zero-padded on the left. 000000 – 999999 %z UTC offset in the form +HHMM or -HHMM. %Z Time zone name. %j Day of the year as a zero-padded decimal number. 001, 002, …, 366 %-j Day of the year as a decimal number. 1, 2, …, 366 %U Week number of the year (Sunday as the first day of the week). All days in a new year preceding the first Sunday are considered to be in week 0. 00, 01, …, 53 %W Week number of the year (Monday as the first day of the week). All days in a new year preceding the first Monday are considered to be in week 0. 00, 01, …, 53 %c Locale’s appropriate date and time representation. Mon Sep 30 07:06:05 2013 %x Locale’s appropriate date representation. 09/30/13 %% A literal ‘%’ character. %