So in PHP I have some code that extracts the day, month and year as an integer with something like this:
// Grab the Date
$date = date("mdy");
$day = (int) substr($date, 2, 2);
$month = (int) substr($date, 0, 2);
$yr = (int) substr($date, 4);
I’d like to do the same in Ruby. And I thought I’d found the answer with
# Grab the Date
now = Date.new(Time.now).to_date
date = Date.parse(now)
day = date.mday
month = date.mon
yr = date.year
I’ve tried variations on this theme and every time it fails with
private method `to_date’ called for Tue Aug 14 01:16:00 -0600 2012:Time (NoMethodError)
I’m sure the answer is somewhere on the web but I’m not putting the right question to Google because I haven’t found it. I’ve only been coding in Ruby off and on for a few months and I imagine it is something simple. So what am I missing?
Thanks
I think you just want:
By the way, you can do that more cleanly, without the
substr()operations, in PHP: