Manju
Manju

Reputation: 115

How to ignore the time while converting string to date?

I want to convert '2011-10-13' to a date object representing the same. I use strptime but it includes the time, which I dont need. How to best avoid this?

d = datetime.datetime.strptime("2011-10-13", "%Y-%m-%d")

print d

2011-10-13 00:00:00

Upvotes: 1

Views: 838

Answers (2)

garnertb
garnertb

Reputation: 9584

You can use the .date() method:

d = datetime.datetime.strptime("2011-10-13", "%Y-%m-%d")
print d.date()
>> 2011-10-13

Upvotes: 4

Matt Ball
Matt Ball

Reputation: 359826

If you don't want a time component, use date, not datetime.

Upvotes: 2

Related Questions