Python datetime.time()方法 (Python datetime.time() Method)
datetime.time() method is used to manipulate objects of datetime class of module datetime.
datetime.time()方法用于操作模块datetime的datetime类的对象。
It uses an instance method and converts it and returns a time object with the same hour, minute, second, microsecond, and fold. tzinfo is None for this method.
它使用实例方法进行转换,并返回具有相同时,分,秒,微秒和倍数的时间对象。 tzinfo对于此方法为None。
Module:
模块:
import datetime
Class:
类:
from datetime import datetime
Syntax:
句法:
time()
Parameter(s):
参数:
None
没有
Return value:
返回值:
The return type of this method is a time object with same hour, minute, second, microsecond and fold. tzinfo is None.
此方法的返回类型是具有相同时,分,秒,微秒和倍数的时间对象。 tzinfo为“无”。
Example:
例:
## Creating a time object from a datetime object
from datetime import datetime
import pytz
## Creating datetime instance
x = datetime(2020, 3, 4,23,12,23,44)
d = x.time()
print("Original object:", x)
print("New time object:",d)
print()
x = datetime.now()
d = x.time()
print("Original date and time", x)
print("New time objec:", d)
print()
x = datetime.today()
d = x.time()
print("Printing the same")
print(x)
print(d)
print()
t = pytz.timezone("Asia/Kolkata")
## Adding tzinfo
x = x.astimezone(t)
d = x.time()
## Note: tzinfo is also not added in the object
## therefor giving a naive time object
print(x)
print(d)
Output
输出量
Original object: 2020-03-04 23:12:23.000044
New time object: 23:12:23.000044
Original date and time 2020-05-03 18:05:23.458667
New time objec: 18:05:23.458667
Printing the same
2020-05-03 18:05:23.458730
18:05:23.458730
2020-05-03 23:35:23.458730+05:30
23:35:23.458730
翻译自: https://www.includehelp.com/python/datetime-time-method-with-example.aspx