从Python的日期时间中提取时间
在本文中,我们将看到如何在Python从 DateTime 中提取时间。
在Python,没有 DateTime 这样的数据类型,首先,我们必须将我们的数据创建为 DateTime 格式,然后我们将我们的 DateTime 数据转换为时间。 Python模块用于将数据转换为 DateTime 格式,但在本文中,我们将使用 datetime 模块来完成此任务。
Syntax: datetime.strptime()
Parameters :
- arg: It can be integer, float, tuple, Series, Dataframe to convert into datetime as its datatype
- format: This will be str, but the default is None. The strftime to parse time, eg “%d/%m/%Y”, note that “%f” will parse all the way up to nanoseconds.
e.g – > format = “%Y%b%d%H%M%S”
e.g., datetime_obj = datetime.strptime(“19022002101010″,”%d%m%Y%H%M%S”) # It will return the datetime object.
从 DateTime 对象中提取时间
在本节中,我们将从 DateTime 对象中提取时间。
我们将字符串转换为 DateTime 对象,现在我们通过调用.time()方法从 DateTime 对象中提取时间。
Syntax : .time()
Returns : It will return the time from the datetime object
下面是实现:
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 对象中提取小时。
Syntax : .hour
Return : It will return the hour from timestamp.
下面是实现:
蟒蛇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")
输出:
You have entered Incorrect Date, please enter a valid date