📜  从字符串创建Python日期时间

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

从字符串创建Python日期时间

在本文中,我们将看到如何从给定的字符串创建一个Python DateTime 对象。

为此,我们将使用datetime.strptime()方法。 strptime() 方法返回一个 date_string 对应的 DateTime 对象,根据用户给出的格式字符串进行解析。

使用 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)


输出:



使用 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)

输出 :

如果字符串参数与格式参数不一致,则 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)

输出:

格式代码列表

格式说明符大部分与 strftime() 方法相同。这些说明符是:

      Directives            Meaning               Example        
%aAbbreviated weekday name.Sun, Mon, …
%AFull weekday name.Sunday, Monday, …
%wWeekday as a decimal number.0, 1, …, 6
%dDay of the month as a zero-padded decimal.01, 02, …, 31
%-dDay of the month as a decimal number.1, 2, …, 30
%bAbbreviated month name.Jan, Feb, …, Dec
%BFull month name.January, February, …
%mMonth as a zero-padded decimal number.01, 02, …, 12
%-mMonth as a decimal number.1, 2, …, 12
%yYear without century as a zero-padded decimal number.00, 01, …, 99
%-yYear without century as a decimal number.0, 1, …, 99
%yYear with century as a decimal number.2013, 2019 etc.
%HHour (24-hour clock) as a zero-padded decimal number.00, 01, …, 23
%-HHour (24-hour clock) as a decimal number.0, 1, …, 23
%IHour (12-hour clock) as a zero-padded decimal number.01, 02, …, 12
%-IHour (12-hour clock) as a decimal number.1, 2, … 12
%pLocale’s AM or PM.AM, PM
%MMinute as a zero-padded decimal number.00, 01, …, 59
%-MMinute as a decimal number.0, 1, …, 59
%SSecond as a zero-padded decimal number.00, 01, …, 59
%-SSecond as a decimal number.0, 1, …, 59
%fMicrosecond as a decimal number, zero-padded on the left.000000 – 999999
%zUTC offset in the form +HHMM or -HHMM. 
%ZTime zone name. 
%jDay of the year as a zero-padded decimal number.001, 002, …, 366
%-jDay of the year as a decimal number.1, 2, …, 366
%UWeek 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
%WWeek 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
%cLocale’s appropriate date and time representation.Mon Sep 30 07:06:05 2013
%xLocale’s appropriate date representation.09/30/13
%%A literal ‘%’ character.%