{"id":2734,"date":"2018-02-09T15:33:21","date_gmt":"2018-02-09T21:33:21","guid":{"rendered":"http:\/\/www.mooreds.com\/wordpress\/?p=2734"},"modified":"2018-02-09T15:33:21","modified_gmt":"2018-02-09T21:33:21","slug":"getting-access-to-the-very-nice-date-functionality-in-non-rails-ruby","status":"publish","type":"post","link":"https:\/\/www.mooreds.com\/wordpress\/archives\/2734","title":{"rendered":"Getting access to the very nice date functionality in non Rails Ruby"},"content":{"rendered":"<p>I am doing some small ruby scripts for a dashboard and need to do some date calculations, like the timestamp of the first of the previous month and the timestamp of the end of the previous month.  Rails makes this so easy with <code>(DateTime.now - 1.month).beginning_of_month<\/code>.  I looked around for a way to do it with straight up Ruby, but didn&#8217;t see a good solution.<\/p>\n<p>Luckily, some of the nice parts of Rails have been broken out into the active support gem.  You need to add it to your <code>Gemfile<\/code> or however else you are managing your gems, of course.  Confusingly, the gem is <code>activesupport<\/code> and the require statement is <code>require active_support\/...<\/code> (see the underscore?).<\/p>\n<p>There&#8217;s an <a href=\"http:\/\/guides.rubyonrails.org\/active_support_core_extensions.html\">entire guide on how to pull in just the active support functionality you need<\/a>.  Unfortunately, I couldn&#8217;t make the targeted includes work (I was trying to pull in both <a href=\"http:\/\/guides.rubyonrails.org\/active_support_core_extensions.html#time\">numeric<\/a> and <a href=\"http:\/\/guides.rubyonrails.org\/active_support_core_extensions.html#extensions-to-date\">date extensions<\/a> precisely, but kept getting the error message <code>undefined method `month' for 1:Integer (NoMethodError)<\/code>.  <\/p>\n<p>Finally, I just pulled in <code>active_support\/time<\/code> and everything worked.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I am doing some small ruby scripts for a dashboard and need to do some date calculations, like the timestamp of the first of the previous month and the timestamp [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10,6,77],"tags":[],"class_list":["post-2734","post","type-post","status-publish","format-standard","hentry","category-dynamic-languages","category-programming","category-rails"],"_links":{"self":[{"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/posts\/2734","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/comments?post=2734"}],"version-history":[{"count":2,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/posts\/2734\/revisions"}],"predecessor-version":[{"id":2736,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/posts\/2734\/revisions\/2736"}],"wp:attachment":[{"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/media?parent=2734"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/categories?post=2734"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/tags?post=2734"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}