[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

[newbie] this month, the next month, and the one after that

Mason Kessinger

1/16/2006 7:45:00 PM

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!

--
Posted via http://www.ruby-....


16 Answers

Jonathan Leighton

1/16/2006 8:23:00 PM

0

On Tue, 2006-01-17 at 04:45 +0900, Mason Kessinger wrote:
> 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!

Well you'd add the month, expressed in seconds, to the current time.
That would be:

irb(main):001:0> puts (Time.now + 60 * 60 * 24 * 30).strftime("Next
month is %B")
Next month is February
=> nil

(Assuming a month is thirty days)

You are probably using Rails (I assume), so you can do this instead:

irb(main):002:0> require_gem 'activerecord'
=> true
irb(main):003:0> puts (Time.now + 1.month).strftime("Next month is %B")
Next month is February
=> nil

Again, that assumes a month is thirty days. A more reliable way would be
to find the next month, work out its length in seconds (precisely), and
then add it to Time.now.

Jon

--
Jonathan Leighton
http://turnips... | http://jonathanlei... | http://digital-...



David Vallner

1/16/2006 8:29:00 PM

0

On Mon, 16 Jan 2006 20:45:18 +0100, Mason Kessinger
<masonkessinger@gmail.com> wrote:

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


My first guess would be requiring "datetime", and then looking up the
month name per DateTime::MONTHNAMES[t.month + 1].

Mind you, DateTime::MONTHNAMES is one-indexed, starting from January, so
you'll have to do some index-shiftingto wrap around the end of the array
correctly. And since there's very little I hate more than shifting array
indices around, the exercise is left to the reader ;P

When I think about it, DateTime::MONTHNAMES[1..12][t.month % 12] should
work perfectly for the next month, with (t.month + 1) for the one after
that, etc.

David Vallner


David Vallner

1/16/2006 8:51:00 PM

0

On Mon, 16 Jan 2006 21:42:52 +0100, Detlef Reichl <detlef.reichl@gmx.org>
wrote:

> Am Dienstag, den 17.01.2006, 04:45 +0900 schrieb Mason Kessinger:
>> 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!
>>
>
> Try this:
>
> irb(main):040:0* t = Time.now
> => Mon Jan 16 21:27:58 CET 2006
> irb(main):041:0> m = t.mon
> => 1
> irb(main):042:0> nt = Time.mktime 2006, m + 1
> => Wed Feb 01 00:00:00 CET 2006
> irb(main):043:0> p nt.strftime("%B")
> "February"
> => nil
> irb(main):044:0> nt = Time.mktime 2006, m + 2
> => Wed Mar 01 00:00:00 CET 2006
> irb(main):045:0> p nt.strftime("%B")
> "March"
> => nil
>
>
> Cheers
> detlef
>
>


Time#mktime. D'oh. I feel so dense for not remembering that one.

David Vallner


James Gray

1/16/2006 8:56:00 PM

0

On Jan 16, 2006, at 2:29 PM, David Vallner wrote:

> Mind you, DateTime::MONTHNAMES is one-indexed, starting from January,

We can fix that. ;)

> so you'll have to do some index-shiftingto wrap around the end of
> the array correctly. And since there's very little I hate more than
> shifting array indices around, the exercise is left to the reader ;P

Okay, I'll bite:

>> current = Time.now.strftime("%B")
=> "January"
>> require "date"
=> true
>> months = (Date::MONTHNAMES + Date::MONTHNAMES).compact
=> ["January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December", "January",
"February", "March", "April", "May", "June", "July", "August",
"September", "October", "November", "December"]
>> three_months = months[months.index(current), 3]
=> ["January", "February", "March"]

James Edward Gray II



konsu

1/16/2006 9:08:00 PM

0

a little off-topic, but a related question: does ruby have support for
date/currency/number localisation? e.g. using comma instead of a dot in
real numbers, month/day names in languages other than english,
localised date format, etc?

thanks
konstantin

Detlef Reichl

1/16/2006 9:09:00 PM

0

Am Dienstag, den 17.01.2006, 05:55 +0900 schrieb James Edward Gray II:
> On Jan 16, 2006, at 2:29 PM, David Vallner wrote:
>
> > Mind you, DateTime::MONTHNAMES is one-indexed, starting from January,
>
> We can fix that. ;)
>
> > so you'll have to do some index-shiftingto wrap around the end of
> > the array correctly. And since there's very little I hate more than
> > shifting array indices around, the exercise is left to the reader ;P
>
> Okay, I'll bite:
>
> >> current = Time.now.strftime("%B")
> => "January"
> >> require "date"
> => true
> >> months = (Date::MONTHNAMES + Date::MONTHNAMES).compact
> => ["January", "February", "March", "April", "May", "June", "July",
> "August", "September", "October", "November", "December", "January",
> "February", "March", "April", "May", "June", "July", "August",
> "September", "October", "November", "December"]
> >> three_months = months[months.index(current), 3]
> => ["January", "February", "March"]
>
i don't like redundant data...


irb(main):042:0* current = Time.now.mon
=> 1
irb(main):043:0> require "date"
=> false
irb(main):044:0> months = Date::MONTHNAMES.compact
=> ["January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December"]
irb(main):045:0> three_month = []
=> []
irb(main):046:0> (0..2).each {|i| three_month << months[(i+current-1) %
12]}
=> 0..2
irb(main):047:0> p three_month
["January", "February", "March"]
=> nil

detlef


> James Edward Gray II
>
>



David Vallner

1/16/2006 9:13:00 PM

0

On Mon, 16 Jan 2006 21:55:40 +0100, James Edward Gray II
<james@grayproductions.net> wrote:

> On Jan 16, 2006, at 2:29 PM, David Vallner wrote:
>
>> Mind you, DateTime::MONTHNAMES is one-indexed, starting from January,
>
> We can fix that. ;)
>
>> so you'll have to do some index-shiftingto wrap around the end of the
>> array correctly. And since there's very little I hate more than
>> shifting array indices around, the exercise is left to the reader ;P
>
> Okay, I'll bite:
>
> >> current = Time.now.strftime("%B")
> => "January"
> >> require "date"
> => true
> >> months = (Date::MONTHNAMES + Date::MONTHNAMES).compact
> => ["January", "February", "March", "April", "May", "June", "July",
> "August", "September", "October", "November", "December", "January",
> "February", "March", "April", "May", "June", "July", "August",
> "September", "October", "November", "December"]
> >> three_months = months[months.index(current), 3]
> => ["January", "February", "March"]
>
> James Edward Gray II
>
>


Ye gods, that's so idiot-proof it ain't pretty anymore amd I let my brain
spin around index shifts.. I've been doing WAAAY too much Java at work.
Help, my 1337 Rüby h@xx0r skills are disappearing! *sic*

David Vallner


Kero van Gelder

1/16/2006 9:35:00 PM

0

On 2006-01-16, Mason Kessinger <masonkessinger@gmail.com> wrote:
> 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!

I can't find out how to print dates, but I do know how to get the next month
without all the assuming-30-days mess.

require 'date'
Date.today.to_s
(Date.today >> 1).to_s
=> "2006-02-16"
(Date.today >> 2).to_s
=> "2006-03-16"

Ara.T.Howard

1/16/2006 10:07:00 PM

0

Ara.T.Howard

1/16/2006 10:09:00 PM

0