{"id":147,"date":"2011-09-21T15:31:57","date_gmt":"2011-09-21T15:31:57","guid":{"rendered":"http:\/\/blogs.law.harvard.edu\/rprasad\/?p=147"},"modified":"2011-09-21T15:37:43","modified_gmt":"2011-09-21T15:37:43","slug":"python-string-to-a-datetime-object","status":"publish","type":"post","link":"https:\/\/archive.blogs.harvard.edu\/rprasad\/2011\/09\/21\/python-string-to-a-datetime-object\/","title":{"rendered":"python: string to a datetime object"},"content":{"rendered":"<p>The task of converting strings to date or date\/time objects arises fairly often.\u00a0 For example, a colleague just had a request for a script to parse a log file so that it may be stored in\u00a0 a database.\u00a0 The log file looks something like this:<\/p>\n<p style=\"padding-left: 30px\">MACHINE001,SOME_USER,<strong>9\/24\/2010 5:03:29 PM<\/strong>,Logged on<br \/>\nMACHINE001,SOME_USER,<strong>9\/24\/2010 5:450:43 PM<\/strong>,Logged off<\/p>\n<p>To convert the date\/time section (boldfaced above) to a usable python object, use the datetime module&#8217;s\u00a0<strong>strptime <\/strong>function.\u00a0 For example:<\/p>\n<pre>&gt;&gt;&gt; from datetime import datetime\r\n&gt;&gt;&gt; dt_str = '9\/24\/2010 5:03:29 PM'\r\n&gt;&gt;&gt; dt_obj = datetime.<strong>strptime<\/strong>(dt_str, '%m\/%d\/%Y %I:%M:%S %p')\r\n&gt;&gt;&gt; dt_obj\r\ndatetime.datetime(2010, 9, 24, 17, 3, 29)<\/pre>\n<p>To go in reverse, use datetime&#8217;s <strong>strftime.\u00a0 <\/strong>Following up on the example above:<\/p>\n<pre>&gt;&gt;&gt; dt_obj.strftime('%A, %B %d, %Y at %H:%M hours')\r\n'Friday, September 24, 2010 at 17:03 hours'<\/pre>\n<p>For full documentation, including definitions for the date directives (%Y, %m, %d, etc.) use the following link:<\/p>\n<p style=\"padding-left: 30px\"><a href=\"http:\/\/docs.python.org\/library\/datetime.html#strftime-and-strptime-behavior\">8.1.7. strftime() and strptime() Behavior<\/a><\/p>\n<p style=\"padding-left: 30px\">(scroll down for directive defintions.\u00a0 e.g. &#8220;%A&#8221; is &#8220;Locale\u2019s full weekday name.&#8221;, etc)<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The task of converting strings to date or date\/time objects arises fairly often.\u00a0 For example, a colleague just had a request for a script to parse a log file so that it may be stored in\u00a0 a database.\u00a0 The log file looks something like this: MACHINE001,SOME_USER,9\/24\/2010 5:03:29 PM,Logged on MACHINE001,SOME_USER,9\/24\/2010 5:450:43 PM,Logged off To convert &hellip; <a href=\"https:\/\/archive.blogs.harvard.edu\/rprasad\/2011\/09\/21\/python-string-to-a-datetime-object\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">python: string to a datetime object<\/span><\/a><\/p>\n","protected":false},"author":3875,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[10585],"tags":[],"class_list":["post-147","post","type-post","status-publish","format-standard","hentry","category-common-tasks"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p4JC3p-2n","_links":{"self":[{"href":"https:\/\/archive.blogs.harvard.edu\/rprasad\/wp-json\/wp\/v2\/posts\/147","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/archive.blogs.harvard.edu\/rprasad\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/archive.blogs.harvard.edu\/rprasad\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/archive.blogs.harvard.edu\/rprasad\/wp-json\/wp\/v2\/users\/3875"}],"replies":[{"embeddable":true,"href":"https:\/\/archive.blogs.harvard.edu\/rprasad\/wp-json\/wp\/v2\/comments?post=147"}],"version-history":[{"count":8,"href":"https:\/\/archive.blogs.harvard.edu\/rprasad\/wp-json\/wp\/v2\/posts\/147\/revisions"}],"predecessor-version":[{"id":155,"href":"https:\/\/archive.blogs.harvard.edu\/rprasad\/wp-json\/wp\/v2\/posts\/147\/revisions\/155"}],"wp:attachment":[{"href":"https:\/\/archive.blogs.harvard.edu\/rprasad\/wp-json\/wp\/v2\/media?parent=147"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/archive.blogs.harvard.edu\/rprasad\/wp-json\/wp\/v2\/categories?post=147"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/archive.blogs.harvard.edu\/rprasad\/wp-json\/wp\/v2\/tags?post=147"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}