📜  Python DateTime – strptime()函数

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

Python DateTime – strptime()函数

strptime()是 DateTime 中可用的另一种方法,用于将字符串格式的时间戳格式化为日期时间对象。

strptime() 如何工作?

这个函数有两个参数,其中一些时间被赋予的字符串和一个格式码,改变字符串入,该字符串被改变为DateTime对象按照下面给出的代码的列表。

格式代码



format codemeaningexample
%aAbbreviated weekday nameSun, Mon
%AFull weekday name Sunday, Monday
%wWeekday as decimal number0…6
%dDay of the month as a zero-padded decimal01, 02
%-dday of the month as decimal number1, 2..
%b Abbreviated month nameJan, Feb
%mmonth as a zero padded decimal number01, 02
%-mmonth as a decimal number1, 2
%B Full month nameJanuary, February
%yyear without century as a zero padded decimal number99, 00 
%-yyear without century as a decimal number0, 99
%Yyear with century as a decimal number2000, 1999
%Hhour(24 hour clock) as a zero padded decimal number01, 23
%-Hhour(24 hour clock) as a decimal number1, 23
%Ihour(12 hour clock) as a zero padded decimal number01, 12
%-Ihour(12 hour clock) as a decimal number1, 12
%plocale’s AM or PMAM, PM
%MMinute as a zero padded decimal number01, 59
%-MMinute as a decimal number1, 59
%SSecond as a zero padded decimal number01, 59
%-SSecond as a decimal number1, 59
%fmicrosecond as a decimal number, zero padded on the left side000000, 999999
%zUTC offset in the form +HHMM or -HHMM 
%ZTime zone name 
%jday of the year as a zero padded decimal number001, 365
%-jday of the year as a decimal number1, 365
%UWeek number of the year (Sunday being the first)0, 6
%WWeek number of the year00, 53
%clocale’s appropriate date and time representationMon Sep 30 07:06:05 2013
%xlocale’s appropriate date representation11/30/98
%Xlocale’s appropriate time representation10:03:43
%%A literal ‘%’ character%

示例 1:使用 strptime 读取日期时间并获取所有时间数据的Python程序。这里我们将获取字符串格式的时间数据并提取小时、分钟、秒和毫秒

Python3
# import datetime module from datetime
from datetime import datetime
 
# consider the time stamp in string format
# DD/MM/YY H:M:S.micros
time_data = "25/05/99 02:35:5.523"
 
# format the string in the given format :
# day/month/year hours/minutes/seconds-micro
# seconds
format_data = "%d/%m/%y %H:%M:%S.%f"
 
# Using strptime with datetime we will format
# string into datetime
date = datetime.strptime(time_data, format_data)
 
# display milli second
print(date.microsecond)
 
# display hour
print(date.hour)
 
# display minute
print(date.minute)
 
# display second
print(date.second)
 
# display date
print(date)


Python3
# import datetime module from datetime
from datetime import datetime
 
# consider the time stamps from a list  in string
# format DD/MM/YY H:M:S.micros
time_data = ["25/05/99 02:35:8.023", "26/05/99 12:45:0.003",
             "27/05/99 07:35:5.523", "28/05/99 05:15:55.523"]
 
# format the string in the given format : day/month/year 
# hours/minutes/seconds-micro seconds
format_data = "%d/%m/%y %H:%M:%S.%f"
 
# Using strptime with datetime we will format string
# into datetime
for i in time_data:
    print(datetime.strptime(i, format_data))


Python3
#import time
import time
 
# get data of 4 th april 2021  at time 9 pm
print(time.strptime('04/04/21 09:31:22', '%d/%m/%y %H:%M:%S'))
 
# get data of 5 th april 2021  at time 9 pm
print(time.strptime('05/04/21 09:00:42', '%d/%m/%y %H:%M:%S'))
 
# get data of 6 th april 2021  at time 9 pm
print(time.strptime('06/04/21 09:11:42', '%d/%m/%y %H:%M:%S'))
 
# get data of 7 th april 2021  at time 9 pm
print(time.strptime('07/04/21 09:41:12', '%d/%m/%y %H:%M:%S'))


Python3
# import the datetime module
import datetime
 
# datetime in string format for may 25 1999
input = '2021/05/25'
 
# format
format = '%Y/%m/%d'
 
# convert from string format to datetime format
datetime = datetime.datetime.strptime(input, format)
 
# get the date from the datetime using date()
# function
print(datetime.date())


Python3
# import the datetime module
import datetime
 
# datetime in string format for list of dates
input = ['2021/05/25', '2020/05/25', '2019/02/15', '1999/02/4']
 
# format
format = '%Y/%m/%d'
for i in input:
   
    # convert from string format to datetime format
    # and get the date
    print(datetime.datetime.strptime(i, format).date())


Python3
#import datetime
from datetime import datetime
 
# consider the datetime string in dd/mm/yyyy
# hh:mm:ss format
date = "25/05/2021 02:35:15"
 
# convert string datetime to  dd/mm/yyyy hh:mm:ss
# format
datetime_date = datetime.strptime(date, "%d/%m/%Y %H:%M:%S")
print(datetime_date)


输出:

示例 2:使用 strptime 的Python代码。这里我们将采用字符串格式的时间数据,并以“%d/%m/%y %H:%M:%S.%f”格式提取时间戳。



蟒蛇3

# import datetime module from datetime
from datetime import datetime
 
# consider the time stamps from a list  in string
# format DD/MM/YY H:M:S.micros
time_data = ["25/05/99 02:35:8.023", "26/05/99 12:45:0.003",
             "27/05/99 07:35:5.523", "28/05/99 05:15:55.523"]
 
# format the string in the given format : day/month/year 
# hours/minutes/seconds-micro seconds
format_data = "%d/%m/%y %H:%M:%S.%f"
 
# Using strptime with datetime we will format string
# into datetime
for i in time_data:
    print(datetime.strptime(i, format_data))

输出:

我们可以通过使用 strptime() 本身来获取遵循所有日期结构的时间。

语法

其中时间戳包括时间和日期

示例:在结构中获取时间的Python代码:



蟒蛇3

#import time
import time
 
# get data of 4 th april 2021  at time 9 pm
print(time.strptime('04/04/21 09:31:22', '%d/%m/%y %H:%M:%S'))
 
# get data of 5 th april 2021  at time 9 pm
print(time.strptime('05/04/21 09:00:42', '%d/%m/%y %H:%M:%S'))
 
# get data of 6 th april 2021  at time 9 pm
print(time.strptime('06/04/21 09:11:42', '%d/%m/%y %H:%M:%S'))
 
# get data of 7 th april 2021  at time 9 pm
print(time.strptime('07/04/21 09:41:12', '%d/%m/%y %H:%M:%S'))

输出:

也可以以 yyyy-mm-dd 日期时间格式获取字符串日期时间。 yyyy-mm-dd 代表年-月-日。我们可以使用 strptime()函数将字符串格式转换为 DateTime。我们将使用 '%Y/%m/%d' 格式将字符串为日期时间。

首先,导入模块并给出输入日期时间字符串。现在使用 strptime 获取所需的格式并使用 date()函数从 DateTime 获取日期

示例 1 :将字符串日期时间格式转换为日期时间的Python程序



蟒蛇3

# import the datetime module
import datetime
 
# datetime in string format for may 25 1999
input = '2021/05/25'
 
# format
format = '%Y/%m/%d'
 
# convert from string format to datetime format
datetime = datetime.datetime.strptime(input, format)
 
# get the date from the datetime using date()
# function
print(datetime.date())

输出:

示例 2:将字符串日期时间列表转换为日期时间

蟒蛇3

# import the datetime module
import datetime
 
# datetime in string format for list of dates
input = ['2021/05/25', '2020/05/25', '2019/02/15', '1999/02/4']
 
# format
format = '%Y/%m/%d'
for i in input:
   
    # convert from string format to datetime format
    # and get the date
    print(datetime.datetime.strptime(i, format).date())

输出:

我们还可以以“%d/%m/%Y %H:%M:%S”格式显示日期时间。为此,我们将以日期-月-年小时:分钟;秒格式获取数据。所以我们必须输入日期时间字符串并获得这种格式

示例3: Python程序将字符串日期时间转换为“%d/%m/%Y %H:%M:%S”格式

蟒蛇3

#import datetime
from datetime import datetime
 
# consider the datetime string in dd/mm/yyyy
# hh:mm:ss format
date = "25/05/2021 02:35:15"
 
# convert string datetime to  dd/mm/yyyy hh:mm:ss
# format
datetime_date = datetime.strptime(date, "%d/%m/%Y %H:%M:%S")
print(datetime_date)

输出: