Convert a string with date and time to a date

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?


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.

Need Your Help

Regular expression to validate City field


I am creating a regular expression to validate a Citiy field. Withe the next code I only can validate a City field with one whitespace:

How to pass data to detail view after being selected in a uitableview cell

objective-c cocoa-touch storyboard

I have a UITableViewController that has data populated from an NSMutableArray called userList. When specific user is selected, it goes to a detail view but it wont display the data in a UITextvie...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.