4

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?

panmireczek
  • 51
  • 1
  • 3

1 Answers1

5

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
>>>