Dik*_*rki 1 python python-3.x jupyter-notebook
我是 python 编程的新手datetime。我正在学习模块。但是,更进一步,我看到:
from datetime import datetime
t = datetime.time()
print ("The current time is ", t)
Run Code Online (Sandbox Code Playgroud)
但是,我得到的错误是:
TypeError Traceback (most recent call last)
<ipython-input-109-91a2f569ccbd> in <module>
8 #print(type(datetime.time()))
9 # Get the current time
---> 10 t = datetime.time()
11
12 print ("The current time is ", t)
TypeError: descriptor 'time' of 'datetime.datetime' object needs an argument
Run Code Online (Sandbox Code Playgroud)
所以我试图在我的 juypter 笔记本中查看日期时间的帮助:
class datetime(date)
| datetime(year, month, day[, hour[, minute[, second[, microsecond[,tzinfo]]]]])
|
| The year, month and day arguments are required. tzinfo may be None, or an
| instance of a tzinfo subclass. The remaining arguments may be ints.
|
| Method resolution order:
| datetime
| date
| builtins.object
|
| Methods defined here:
|
|
| time(...)
| Return time object with same time but with tzinfo=None.
|
|
| now(tz=None) from builtins.type
| Returns new datetime object representing current time local to tz.
|
| tz
| Timezone object.
|
| If no tz is specified, uses local timezone.
|
Run Code Online (Sandbox Code Playgroud)
我看到类datetime具有功能时间。正如 juypter notebook 给出的:
time(...)
| Return time object with same time but with tzinfo=None.
Run Code Online (Sandbox Code Playgroud)
(我没有发布上面的所有函数)。我对函数参数期望什么感到困惑?这三个点我不清楚。这三个点是什么意思?
所以,我转向了 python 文档和https://docs.python.org/3/library/datetime.html#datetime.datetime.year.
而我看到,
datetime.time()
Return time object with same hour, minute, second, microsecond and fold. tzinfo is None. See also method timetz().
Changed in version 3.6: The fold value is copied to the returned time object.
Run Code Online (Sandbox Code Playgroud)
所以,在这个文档中,我看到time()函数没有它所期望的任何参数。此时我再次感到困惑。因为,错误说它至少需要一个参数,但在文档中它向我展示了该函数不期待任何争论。有人可以澄清我如何从 juypter notebook 看到该功能的期望吗?
在您的代码中,您试图直接从时间对象中获取时间,因此您需要执行以下操作:
from datetime import datetime
t = datetime.now().time()
print ("The current time is ", t)
# The current time is 18:11:56.271907
Run Code Online (Sandbox Code Playgroud)
所述类方法 datetime.now()被描述在这里
返回当前本地日期和时间。如果可选参数 tz 为 None 或未指定,则类似于 today(),但是,如果可能,提供的精度比通过 time.time() 时间戳获得的精度更高(例如,这可能在提供C gettimeofday() 函数)。
如果要time()直接使用该功能,则需要更改导入:
import datetime
print(datetime.time())
# datetime.time(0, 0)
Run Code Online (Sandbox Code Playgroud)
进口from datetime import datetime意味着要导入的对象 日期时间从模块日期时间,到这个对象你有嵌套对象 的时间 <method 'time' of 'datetime.datetime' objects>。
datetime 模块可能特别令人困惑,因为该模块还包含一个 datetime 类,因此datetime.datetime在 datetime 模块中指的是 datetime 类。如果这样做from datetime import datetime,您只是在导入 datetime 类,因此当您在代码中引用 datetime 时,它指的是 datetime 类,而不是整个 datetime 模块。
使用 importimport datetime直接导入模块time(),使用类的函数在哪里 <class 'datetime.time'>
参考时间类是这里
一个理想化的时间,独立于任何特定的一天,假设每天正好有 24 60 60 秒(这里没有“闰秒”的概念)。属性:小时、分钟、秒、微秒和 tzinfo。
对嵌套在datetime 模块中的datetime 类的引用是here
日期和时间的组合。属性:年、月、日、小时、分钟、秒、微秒和 tzinfo。
在此参考中,您可以找到datetime.time() 实例方法
返回具有相同时、分、秒、微秒和折叠的时间对象。tzinfo 是无。另见方法 timetz()。
| 归档时间: |
|
| 查看次数: |
130 次 |
| 最近记录: |