{"id":2694,"date":"2015-06-07T19:31:35","date_gmt":"2015-06-07T19:31:35","guid":{"rendered":"http:\/\/wiki.davelevy.info\/?p=2694"},"modified":"2015-06-13T08:35:00","modified_gmt":"2015-06-13T08:35:00","slug":"converting-dates-to-strings-using-python","status":"publish","type":"post","link":"https:\/\/davelevy.info\/wiki\/converting-dates-to-strings-using-python\/","title":{"rendered":"Converting dates to strings using Python"},"content":{"rendered":"<p>While working on <a href=\"https:\/\/davelevy.info\/wiki\/exporting-ello-co\/\">converting ello.json to rss<\/a>, I needed to convert the date formats, I needed to do it in Python and found the following code worked, using <code>strptime<\/code> and <code>strfmtime<\/code>. I had to augment the date string with the name of the day of the week attribute, so felt it had to via a date data type. (This is an important part of the requirement, since otherwise string manipulation would have been enough.) Finding exampes was hard, so I hope it helps,<!--more--><\/p>\n<p>Here&#8217;s the code<\/p>\n<pre>from datetime import datetime\r\n\r\ndef stripz(s):\r\n\treturn s.strip('Z')\r\n\r\nfmt='%Y-%m-%dT%H:%M:%S.%f'\r\nofmt='%a, %d %b %Y %H:%M:%S +0000' \r\n\r\nedate=stripz(\"2015-06-02T13:49:29.761Z\") # shows the input format\r\ndto=datetime.strptime(edate,fmt)\r\npubdate=datetime.strftime(dto, ofmt)\r\n\r\n<\/pre>\n<p>Dunno if I need to cope with the TZ as I did, which is to strip it out and write it as a constant but I am getting empty timezone data fields and strptime was barfing. pubdate is a string. This <a href=\"https:\/\/docs.python.org\/2\/library\/datetime.html#strftime-and-strptime-behavior\">page in the python documentation<\/a> helped.<\/p>\n<p>This could almost certainly be done on one line and the function removed, but I find it easier to build this way and some COBOL habits are hard to drop.<\/p>\n<p>Image Credit: <a href=\"https:\/\/www.flickr.com\/photos\/studiocurve\/13080208\/\">@flickr &#8216;studio curve&#8217; CC BY-NC-ND 2005<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>While working on converting ello.json to rss, I needed to convert the date formats, I needed to do it in Python and found the following code worked, using strptime and strfmtime. I had to augment the date string with the name of the day of the week attribute, so felt it had to via a&#8230;<\/p>\n","protected":false},"author":1,"featured_media":2696,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","_share_on_mastodon":"0"},"categories":[3],"tags":[458,36,45,911],"class_list":["post-2694","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology","tag-datetime","tag-programming","tag-python","tag-technology"],"share_on_mastodon":{"url":"","error":""},"jetpack_featured_media_url":"https:\/\/davelevy.info\/wiki\/wp-content\/uploads\/2015\/06\/calendar1-w640.png","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/davelevy.info\/wiki\/wp-json\/wp\/v2\/posts\/2694","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/davelevy.info\/wiki\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/davelevy.info\/wiki\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/davelevy.info\/wiki\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/davelevy.info\/wiki\/wp-json\/wp\/v2\/comments?post=2694"}],"version-history":[{"count":1,"href":"https:\/\/davelevy.info\/wiki\/wp-json\/wp\/v2\/posts\/2694\/revisions"}],"predecessor-version":[{"id":2701,"href":"https:\/\/davelevy.info\/wiki\/wp-json\/wp\/v2\/posts\/2694\/revisions\/2701"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/davelevy.info\/wiki\/wp-json\/wp\/v2\/media\/2696"}],"wp:attachment":[{"href":"https:\/\/davelevy.info\/wiki\/wp-json\/wp\/v2\/media?parent=2694"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/davelevy.info\/wiki\/wp-json\/wp\/v2\/categories?post=2694"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/davelevy.info\/wiki\/wp-json\/wp\/v2\/tags?post=2694"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}