From: A LeDonne Date: 2006-01-18T00:30:04+09:00 Subject: Re: [newbie] this month, the next month, and the one after that On 1/16/06, Detlef Reichl 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 > Along with Time.mktime, there are Date methods to shift dates by a number of months... Date#>> and Date#<< . Which you use depends on whether you need Dates or Times. irb(main):022:0> require 'date' => true irb(main):023:0> bob = Date.today() => # irb(main):024:0> puts bob.strftime() 2006-01-17 => nil irb(main):025:0> puts bob.strftime("%B") January => nil irb(main):026:0> nextmonth = bob >> 1 => # irb(main):027:0> puts nextmonth.strftime() 2006-02-17 => nil irb(main):028:0> puts nextmonth.strftime("%B") February => nil irb(main):029:0> one_after_that = bob >> 2 => # irb(main):030:0> puts one_after_that.strftime() 2006-03-17 => nil irb(main):031:0> puts one_after_that.strftime("%B") March => nil -A