Python – Convert a string with date and time to a date

datetimepython

I've got this date time string:

post["date"] = "2007-07-18 10:03:19"

I'd like to extract just "2007-07-18" as a date. I've seen some reference to strptime but I'm not sure how to use it. How can I extract the date from this string?

Best Solution

The other two answers are fine, but if you actually want the date for something else, you can use the datetime module:

from datetime import datetime
d = datetime.strptime('2007-07-18 10:03:19', '%Y-%m-%d %H:%M:%S')
day_string = d.strftime('%Y-%m-%d')

It might be overkill for now, but it'll come in useful. You can see all of the format specifiers here.