Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display Python datetime without time

I have a date string and want to convert it to the date type:

I have tried to use datetime.datetime.strptime with the format that I want but it is returning the time with the conversion.

    when = alldates[int(daypos[0])]     print when, type(when)      then = datetime.datetime.strptime(when, '%Y-%m-%d')     print then, type(then) 

This is what the output returns:

   2013-05-07 <type 'str'>    2013-05-07 00:00:00 <type 'datetime.datetime'> 

I need to remove the the time: 00:00:00.

like image 869
pythonhunter Avatar asked Oct 02 '14 02:10

pythonhunter


People also ask

How do I display a date without time in Python?

To remove the time from a datetime object in Python, convert the datetime to a date using date(). You can also use strftime() to create a string from a datetime object without the time.


1 Answers

print then.date() 

What you want is a datetime.date object. What you have is a datetime.datetime object. You can either change the object when you print as per above, or do the following when creating the object:

then = datetime.datetime.strptime(when, '%Y-%m-%d').date() 
like image 184
Woody Pride Avatar answered Oct 02 '22 18:10

Woody Pride