{"id":223,"date":"2010-10-07T14:18:24","date_gmt":"2010-10-07T14:18:24","guid":{"rendered":"http:\/\/nicholshayes.co.uk\/blog\/?p=223"},"modified":"2015-03-02T11:23:33","modified_gmt":"2015-03-02T11:23:33","slug":"time-format","status":"publish","type":"post","link":"http:\/\/nicholshayes.co.uk\/blog\/?p=223","title":{"rendered":"Time format"},"content":{"rendered":"<p>I found an <a href=\"http:\/\/snippets.dzone.com\/posts\/show\/2406\">interesting article by Todd Sayre<\/a> on time formats in Rails. It is only short, but interesting. Based on that I created config\/initializers\/time_formats.rb in my latest app:<\/p>\n<div class=\"codecolorer-container ruby default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"ruby codecolorer\"><span class=\"co1\"># Used to create time and datetime formatting short cuts. Search for to_formatted_s in the api<\/span><br \/>\n<span class=\"co1\"># Also see http:\/\/snippets.dzone.com\/posts\/show\/2406<\/span><br \/>\n<br \/>\n<span class=\"co1\"># If date = Time.local(2009,12,24,15,30,27)<\/span><br \/>\n<br \/>\n<span class=\"kw4\">Time<\/span>::DATE_FORMATS<span class=\"br0\">&#91;<\/span><span class=\"re3\">:datetime<\/span><span class=\"br0\">&#93;<\/span> = <span class=\"st0\">&quot;%d-%b-%Y %H:%M&quot;<\/span> &nbsp;<span class=\"co1\"># date.to_s(:datetime) &nbsp;----&gt; &nbsp;24-Dec-2009 15:30<\/span><br \/>\n<span class=\"kw4\">Time<\/span>::DATE_FORMATS<span class=\"br0\">&#91;<\/span><span class=\"re3\">:date<\/span><span class=\"br0\">&#93;<\/span> = <span class=\"st0\">&quot;%d-%b-%Y&quot;<\/span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class=\"co1\"># date.to_s(:date) &nbsp; &nbsp; &nbsp;----&gt; &nbsp;24-Dec-2009<\/span><br \/>\n<span class=\"kw4\">Time<\/span>::DATE_FORMATS<span class=\"br0\">&#91;<\/span><span class=\"re3\">:time<\/span><span class=\"br0\">&#93;<\/span> = <span class=\"st0\">&quot;%H:%M&quot;<\/span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"co1\"># date.to_s(:time) &nbsp; &nbsp; &nbsp;----&gt; &nbsp;15:30<\/span><br \/>\n<span class=\"kw4\">Time<\/span>::DATE_FORMATS<span class=\"br0\">&#91;<\/span><span class=\"re3\">:default<\/span><span class=\"br0\">&#93;<\/span> = <span class=\"kw4\">Time<\/span>::DATE_FORMATS<span class=\"br0\">&#91;<\/span><span class=\"re3\">:datetime<\/span><span class=\"br0\">&#93;<\/span> &nbsp; <span class=\"co1\"># Sets default behaviour for date.to_s<\/span><\/div><\/div>\n<p>I think the code speaks for itself. As you can see it is very easy to extend Time#to_s to provide straight forward formatting. It is also easy to overwrite the default<\/p>\n<h3>Update<\/h3>\n<p>Note that the previous format only affected Time objects. Date object text formats have to be defined separately. However, if you want the two to share a common format, this is a useful technique:<\/p>\n<div class=\"codecolorer-container ruby default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"ruby codecolorer\">date_formats = <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; concise: <span class=\"st0\">'%d-%b-%Y'<\/span> <span class=\"co1\"># 13-Jan-2014<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><br \/>\n<br \/>\n<span class=\"kw4\">Time<\/span>::DATE_FORMATS.<span class=\"me1\">merge<\/span>! date_formats<br \/>\n<span class=\"kw4\">Date<\/span>::DATE_FORMATS.<span class=\"me1\">merge<\/span>! date_formats<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>I found an interesting article by Todd Sayre on time formats in Rails. It is only short, but interesting. Based on that I created config\/initializers\/time_formats.rb in my latest app: # Used to create time and datetime formatting short cuts. Search &hellip; <a href=\"http:\/\/nicholshayes.co.uk\/blog\/?p=223\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[],"_links":{"self":[{"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/223"}],"collection":[{"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=223"}],"version-history":[{"count":5,"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/223\/revisions"}],"predecessor-version":[{"id":523,"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/223\/revisions\/523"}],"wp:attachment":[{"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=223"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=223"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=223"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}