📜  从Python的日期时间中提取时间

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

从Python的日期时间中提取时间

在本文中,我们将看到如何在Python从 DateTime 中提取时间。

在Python,没有 DateTime 这样的数据类型,首先,我们必须将我们的数据创建为 DateTime 格式,然后我们将我们的 DateTime 数据转换为时间。 Python模块用于将数据转换为 DateTime 格式,但在本文中,我们将使用 datetime 模块来完成此任务。

从 DateTime 对象中提取时间

在本节中,我们将从 DateTime 对象中提取时间。

我们将字符串转换为 DateTime 对象,现在我们通过调用.time()方法从 DateTime 对象中提取时间。

下面是实现:

Python3
# import important module
import datetime
from datetime import datetime
  
# Create datetime string
datetime_str = "24AUG2001101010"
  
# call datetime.strptime to convert
# it into datetime datatype
datetime_obj = datetime.strptime(datetime_str, 
                                 "%d%b%Y%H%M%S")
  
# It will print the datetime object
print(datetime_obj)
  
# extract the time from datetime_obj
time = datetime_obj.time()
  
  
# it will print time that 
# we have extracted from datetime obj
print(time)


Python3
# import important module
import datetime
from datetime import datetime
  
# Create datetime string
datetime_str = "31OCT2020231032"
  
# call datetime.strptime to convert
# it into datetime datatype
datetime_obj = datetime.strptime(datetime_str, "%d%b%Y%H%M%S")
  
# It will print the datetime object
print("date time : {}".format(datetime_obj))
  
# extract the time from datetime_obj
time = datetime_obj.time()
  
# it will print time that
# we have extracted from datetime obj
print("Time : {}".format(time)) 
  
# extract hour from time
hour = time.hour
print("Hour : {}".format(hour))


Python3
# import important module
import datetime
from datetime import datetime
  
# Create datetime string
datetime_str = "10JAN300123000"
  
# call datetime.strptime to
# convert it into datetime datatype
datetime_obj = datetime.strptime(datetime_str,
                                 "%d%b%Y%H%M%S")
  
# It will print the datetime object
print("date time : {}".format(datetime_obj))
  
# extract the time from datetime_obj
time = datetime_obj.time()
  
# it will print time that we
# have extracted from datetime obj
print("Time : {}".format(time)) 
  
# extract minute from time
minute = time.minute
print("Minute : {}".format(minute))
  
# extract second from time
second = time.second
print("Second : {}".format(second))


Python3
# import important module
import datetime
from datetime import datetime
  
# Create datetime string
datetime_str = "32JAN2022102356"
try:
    
    # call datetime.strptime to convert it into
    # datetime datatype
    datetime_obj = datetime.strptime(datetime_str,
                                     "%d%b%Y%H%M%S")
      
    # It will print the datetime object
    print("date time : {}".format(datetime_obj))
except:
    print("You have entered Incorrect Date, please enter a valid date")


输出:

2001-08-24 10:10:10
10:10:10

从时间中提取小时

在本节中,我们将从 DateTime 对象中提取时间中提取小时,所有 3 个步骤都与前面的示例相同,在此示例中,我们将添加 .hour 方法以从 DateTime 对象中提取小时。



我们必须提取时间,所以下一部分是通过调用 .hour 方法从 DateTime 对象中提取小时。

下面是实现:

蟒蛇3

# import important module
import datetime
from datetime import datetime
  
# Create datetime string
datetime_str = "31OCT2020231032"
  
# call datetime.strptime to convert
# it into datetime datatype
datetime_obj = datetime.strptime(datetime_str, "%d%b%Y%H%M%S")
  
# It will print the datetime object
print("date time : {}".format(datetime_obj))
  
# extract the time from datetime_obj
time = datetime_obj.time()
  
# it will print time that
# we have extracted from datetime obj
print("Time : {}".format(time)) 
  
# extract hour from time
hour = time.hour
print("Hour : {}".format(hour))

输出:

date time : 2020-10-31 23:10:32
Time : 23:10:32
Hour : 23

从时间中提取分钟和秒

在此示例中,我们将添加 .minute, .second 方法以从 DateTime 对象中提取分钟和秒。

蟒蛇3

# import important module
import datetime
from datetime import datetime
  
# Create datetime string
datetime_str = "10JAN300123000"
  
# call datetime.strptime to
# convert it into datetime datatype
datetime_obj = datetime.strptime(datetime_str,
                                 "%d%b%Y%H%M%S")
  
# It will print the datetime object
print("date time : {}".format(datetime_obj))
  
# extract the time from datetime_obj
time = datetime_obj.time()
  
# it will print time that we
# have extracted from datetime obj
print("Time : {}".format(time)) 
  
# extract minute from time
minute = time.minute
print("Minute : {}".format(minute))
  
# extract second from time
second = time.second
print("Second : {}".format(second))

输出

date time : 3001-01-10 23:00:00
Time : 23:00:00
Minute : 0
Second : 0

确定时间,如果日期注释在范围内

如果 Date 不在范围内,那么代码会给我们一个“ValueError”。为了确定出现此错误的时间,我们可以做的是使用异常语句并告诉用户您输入了错误的日期并且无法从该日期获取时间,因为该日期从未存在过,那么如何时间应该存在于那个日期。

蟒蛇3

# import important module
import datetime
from datetime import datetime
  
# Create datetime string
datetime_str = "32JAN2022102356"
try:
    
    # call datetime.strptime to convert it into
    # datetime datatype
    datetime_obj = datetime.strptime(datetime_str,
                                     "%d%b%Y%H%M%S")
      
    # It will print the datetime object
    print("date time : {}".format(datetime_obj))
except:
    print("You have entered Incorrect Date, please enter a valid date")

输出: