Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Date format without hour, minutes and seconds [duplicate]

I have following code:

import datetime
x = '2015-01-01'
date = datetime.datetime.strptime(x, "%Y-%m-%d")
print(date)

The problem is that I want to print date like "2015-01-01" and not "2015-01-01 00:00:00". How to print it without hours, minutes and seconds?

like image 660
panmireczek Avatar asked Sep 13 '25 07:09

panmireczek


1 Answers

datetime objects have a date method which does this for you:

>>> import datetime
>>> x = '2015-01-01'
>>> date = datetime.datetime.strptime(x, "%Y-%m-%d")
>>> print(date.date())
2015-01-01
>>>