[newbie] This Month / Next Month / The One After That


#1

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!


#2

On 16 Jan '06, at 11:53 AM, Mason K. 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 :slight_smile:

*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


#3

2006/1/16, Jens A. removed_email_address@domain.invalid:

*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 !


#4

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


#5

But how to change month names ? to obtain french month for instance…


#6

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 :wink:

pm


#7

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 :slight_smile:

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") %>

:slight_smile:


#8

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 :wink:

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> :frowning:
irb(main):007:1*


#9

“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-for-rubys-time-strftime

Thanks a lot