📌  相关文章
📜  使用时区将字符串转换为Python的日期时间

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

使用时区将字符串转换为Python的日期时间

先决条件:日期时间模块

在本文中,我们将学习如何使用Python从时间字符串获取日期时间对象。

要将时间字符串转换为日期时间对象,使用 datetime 模块的 datetime.strptime()函数。此函数返回日期时间对象。

可以传递给 strptime() 的可接受格式列表:

DirectiveMeaningOutput Format
%YAbbreviated weekday name.Sun, Mon, …, Sat
%mMonth as a zero-padded decimal number.01, 02, …, 12
%bMonth as locale’s abbreviated name.Jan, Feb, …, Dec
%dDay of the month as a zero-padded decimal number.01, 02, …, 31
%HHour (24-hour clock) as a zero-padded decimal number. 00, 01, …, 23
%IHour (12-hour clock) as a zero-padded decimal number.01, 02, …, 12
%MMinute as a zero-padded decimal number.00, 01, …, 59
%SSecond as a zero-padded decimal number.00, 01, …, 59
%fMicrosecond as a decimal number, zero-padded on the left.000000, 000001, …, 999999
%pLocale’s equivalent of either AM or PMAM, PM, am, pm
%zUTC offset in the form ±HHMM+0000, -0400, +0530

示例 1:将日期时间字符串转换为日期时间

Python3
# Python3 code to demonstrate
# Getting datetime object using a date_string
  
# importing datetime module
import datetime
  
# datestring for which datetime_obj required
date_string = '2021-09-01 15:27:05.004573 +0530'
print("string datetime: ")
print(date_string)
print("datestring class is :", type(date_string))
  
# using strptime() to get datetime object
datetime_obj = datetime.datetime.strptime(
    date_string, '%Y-%m-%d %H:%M:%S.%f %z')
  
print("converted to datetime:")
  
# Printing datetime
print(datetime_obj)
  
# Checking class of datetime_obj.
print("datetime_obj class is :", type(datetime_obj))


Python3
# Python3 code to demonstrate
# Getting datetime object using a date_string
  
# importing datetime module
import datetime
  
# datestring for which datetime_obj required
date_string = '2021-09-01 15:27:05'
  
# using strptime() to get datetime object
datetime_obj = datetime.datetime.strptime(date_string, '%Y-%m-%d %H:%M:%S')
  
# Printing datetime
print(datetime_obj)


Python3
# Python3 code to demonstrate
# Getting datetime object using a date_string
  
# importing datetime module
import datetime
  
# datestring for which datetime_obj required
date_string = 'Sep 01 2021 03:27:05 PM'
  
# using strptime() to get datetime object
datetime_obj = datetime.datetime.strptime(date_string, '%b %d %Y %I:%M:%S %p')
  
# Printing datetime
print(datetime_obj)


输出:

string datetime: 
2021-09-01 15:27:05.004573 +0530
datestring class is : 
converted to datetime:
2021-09-01 15:27:05.004573+05:30
datetime_obj class is : 

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

蟒蛇3

# Python3 code to demonstrate
# Getting datetime object using a date_string
  
# importing datetime module
import datetime
  
# datestring for which datetime_obj required
date_string = '2021-09-01 15:27:05'
  
# using strptime() to get datetime object
datetime_obj = datetime.datetime.strptime(date_string, '%Y-%m-%d %H:%M:%S')
  
# Printing datetime
print(datetime_obj)

输出:



在此示例中,从第一个示例中删除了微秒和时区部分,因此我们还需要从格式字符串删除微秒和时区缩写

示例 3:将日期时间字符串转换为日期时间

蟒蛇3

# Python3 code to demonstrate
# Getting datetime object using a date_string
  
# importing datetime module
import datetime
  
# datestring for which datetime_obj required
date_string = 'Sep 01 2021 03:27:05 PM'
  
# using strptime() to get datetime object
datetime_obj = datetime.datetime.strptime(date_string, '%b %d %Y %I:%M:%S %p')
  
# Printing datetime
print(datetime_obj)

输出: