Vivek Bhintade
Vivek Bhintade

Reputation: 383

How to convert datetime output datetime.datetime to datetime?

I wrote the following code and it gives output of current time in format datetime.datetime.('current date and time') , But i want output to be as datetime('current date and time').

I want to remove preceding "Datetime" , tried it with "split" but its not working ,gives the following error "datetime.datetime' object has no attribute 'split".

does anyone know how to do it in python?

thanks in advance.

{

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

}

Upvotes: 1

Views: 1931

Answers (1)

Hussain
Hussain

Reputation: 5177

Because split will only work on a String not datetime.datetime. Here this will clear out your confusion -

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'

Upvotes: 4

Related Questions