Forum: Ruby on Rails [newbie] This Month / Next Month / The One After That

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Fb8f512d95fa45e52f7cdd02e00d6c9f?d=identicon&s=25 Mason Kessinger (Guest)
on 2006-01-16 20:53
What I want to do is simple.

I'd like code that prints out the name of this month (January) the next
month (February) and the one after that (March)

I've been able to print this month:

<% t = Time.now %>
<%= t.strftime(" This Month is: %B") %>

But I can't figure out how to get the next months to print out!
Edbd5f1c2f535b14165ae883fa7c3f37?d=identicon&s=25 Jens Alfke (Guest)
on 2006-01-17 01:03
(Received via mailing list)
On 16 Jan '06, at 11:53 AM, Mason Kessinger wrote:

> I'd like code that prints out the name of this month (January) the
> next
> month (February) and the one after that (March)

I was delving into the time/date morass over the weekend*, so now I
get to sound like an expert:

Date.today.mon returns the current month number (1..12).
Date::MONTHNAMES is an array of month names.

Don't forget to use modular arthmetic, of course :)

*And may I take a moment to gripe about how confusing it is that Ruby
has entirely distinct Time and Date classes, and that there is no
obvious way to convert between them? I spent about an hour on
Saturday trying to find a way to compute the number of days between
two Times.

--Jens
3dd4b52a0946bd698b1d1635a46ea3a3?d=identicon&s=25 Francois Beausoleil (Guest)
on 2006-01-17 01:12
(Received via mailing list)
2006/1/16, Jens Alfke <jens@mooseyard.com>:
> *And may I take a moment to gripe about how confusing it is that Ruby has
> entirely distinct Time and Date classes, and that there is no obvious way to
> convert between them? I spent about an hour on Saturday trying to find a way
> to compute the number of days between two Times.

Look no further than ActiveSupport:

Date#to_time # returns self, converted to a Time @ midnight
Date#to_date # returns self
Time#to_date # returns self, converted to a Date
Time#to_time # returns self

Hope that helps !
Fb8f512d95fa45e52f7cdd02e00d6c9f?d=identicon&s=25 Mason Kessinger (Guest)
on 2006-01-17 01:15
Thanks.

I've only been at this for a few weeks in my spare time so this will
save me much headache.

I really appreciate your help!

mk
3dfb09682a075fb098dd01c4a8b1b3cf?d=identicon&s=25 pm (Guest)
on 2006-01-23 16:37
But how to change month names ? to obtain french month for instance..
71f1b6b2c3fd9af2e8c52618fb91caa6?d=identicon&s=25 Jules Jacobs (jules)
on 2006-01-23 17:02
pm wrote:
> But how to change month names ? to obtain french month for instance..

Just use your own array instead of Date::MONTHNAMES.

FRENCH_MONTHNAMES = ['Janvier', 'Fevrier', ... , 'Decembre']

> I was delving into the time/date morass over the weekend*, so now I
> get to sound like an expert:
> Date.today.mon returns the current month number (1..12).
> Date::MONTHNAMES is an array of month names.
> Don't forget to use modular arthmetic, of course :)

next = 1.month.from_now
nextnext = 2.months.from_now

<%= next.strftime(" Next Month is: %B") %>
<%= nextnext.strftime(" The Month After That One is: %B") %>

:)
3dfb09682a075fb098dd01c4a8b1b3cf?d=identicon&s=25 pm (Guest)
on 2006-01-23 17:41
thank you for your help.

>> But how to change month names ? to obtain french month for instance..
>
> Just use your own array instead of Date::MONTHNAMES.
>
> FRENCH_MONTHNAMES = ['Janvier', 'Fevrier', ... , 'Decembre']
>

I can use FRENCH_MONTHNAMES like a table..
but I want to replace %B in the expression.. and i can't =(

thanks ;)

pm
71f1b6b2c3fd9af2e8c52618fb91caa6?d=identicon&s=25 Jules Jacobs (jules)
on 2006-01-23 18:29
pm wrote:
> thank you for your help.
>
>>> But how to change month names ? to obtain french month for instance..
>>
>> Just use your own array instead of Date::MONTHNAMES.
>>
>> FRENCH_MONTHNAMES = ['Janvier', 'Fevrier', ... , 'Decembre']
>>
>
> I can use FRENCH_MONTHNAMES like a table..
> but I want to replace %B in the expression.. and i can't =(
>
> thanks ;)
>
> pm

irb(main):001:0> Date::MONTHNAMES
=> [nil, "January", "February", "March", "April", "May", "June", "July",
"August
", "September", "October", "November", "December"]
irb(main):002:0> Date::MONTHNAMES[1] = 'Janvier'
=> "Janvier"
irb(main):003:0> Date::MONTHNAMES[1]
=> "Janvier"
irb(main):004:0> Time.now
=> Mon Jan 23 18:27:28 West-Europa (standaardtijd) 2006
irb(main):005:0> Time.now.strftime("%B")
=> "January"
irb(main):006:0> :(
irb(main):007:1*
3dfb09682a075fb098dd01c4a8b1b3cf?d=identicon&s=25 pm (Guest)
on 2006-01-24 11:06
"And finally, everything worked out just fine.
Christmas was saved, though there wasn't much time.
But after that night, things were never the same"
Danny Elfman =)

I have found the solution.. I have override strftime with this code =)
And it's so great =)

http://poocs.net/articles/2005/10/04/localization-...

Thanks a lot
This topic is locked and can not be replied to.