1

我编写了以下代码,它以格式输出当前时间datetime.datetime.('current date and time'),但我希望输出为datetime('current date and time').

我想删除前面的“日期时间” ,用“拆分”尝试过,但它不起作用,出现以下错误 "datetime.datetime' object has no attribute 'split"

有谁知道如何在python中做到这一点?

提前致谢。

{

from datetime import datetime
test = datetime.now()
test.split('.')[0]

}
4

1 回答 1

4

因为split仅适用于 String 而不是datetime.datetime。在这里,这将消除您的困惑-

hussain@work-desktop:~$ python
Python 2.7.1+ (r271:86832, Sep 27 2012, 21:16:52) 
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from datetime import datetime
>>> test = datetime.now()
>>> print test
2013-10-23 11:49:28.385757
>>> test.split('.')[0]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'datetime.datetime' object has no attribute 'split'
>>> type(test)
<type 'datetime.datetime'>
>>> test.strftime('%Y-%m-%d %H:%M:%S')
'2013-10-23 11:49:28'
于 2013-10-23T06:05:40.560 回答