Week number

Hi All,

I’m newbie on Ruby and I like to know how I can manage to print out the
week numbers(counting from monday or sunday). Till now, I can’t find any
method for it. Can somebody please help.

Many Thanks

Ilias None wrote:

Hi All,

I’m newbie on Ruby and I like to know how I can manage to print out the
week numbers(counting from monday or sunday). Till now, I can’t find any
method for it. Can somebody please help.

Many Thanks

Dear Ilias,
What you are coming to say.
I can’t understand the question fully.
Please express your thoughts what you have in your mind.

by
vellingiri

Hi,

Am Mittwoch, 19. Sep 2007, 19:44:24 +0900 schrieb Ilias None:

I’m newbie on Ruby and I like to know how I can manage to print out the
week numbers(counting from monday or sunday).

require “date”
d = Date.today
puts d.cwday, d.cweek, c.cwyear

Bertram

Thanks Bertram!

Bertram S. wrote:

Hi,

Am Mittwoch, 19. Sep 2007, 19:44:24 +0900 schrieb Ilias None:

I’m newbie on Ruby and I like to know how I can manage to print out the
week numbers(counting from monday or sunday).

require “date”
d = Date.today
puts d.cwday, d.cweek, c.cwyear

Bertram

Pr Rm wrote:

Hello,

How can I calculate the week dates using a week number ?

EG:
Input (Week Number):
22

Output:
06/12/08 - 06/19/08

Thanks

Have a look at Date#commercial (
http://www.ruby-doc.org/stdlib/libdoc/date/rdoc/classes/Date.html#M000158
)

require ‘date’

def week_dates( week_num )
year = Time.now.year
week_start = Date.commercial( year, week_num, 1 )
week_end = Date.commercial( year, week_num, 7 )
week_start.strftime( “%m/%d/%y” ) + ’ - ’ + week_end.strftime(
“%m/%d/%y” )
end

puts week_dates(22)

hth,

Siep

Maybe this is also a goood way :slight_smile:

With this you can get the weeknumber of a date like

Time.now.kw => 11

or just create the starttime of a week(starting on monday -> for sundy
set wd to Zero)

t = Time.newkw(21) => Mon May 23 01:00:00 +0200 2011

Fügt der Timeklasse Kalenderwochen beginnend bei Montag ein.

class Time
def kw(wd=1)
kw = self.strftime("%U").to_i
kw -= 1 if self.wday < (wd%7)
return kw
end
def self.newkw(kw=0, year=Time.now.year.to_i, wd=1)
# Gibt Timeobjekt zurück, welches den Start der KW ausgibt
t = Time.local(year)
(0…364).each { |n|
if t.kw(wd) == kw; break; end
t += 3600*24
}
return t
end
end

puts "\n\n\nDie Kalenderwoche " + Time.now.kw.to_s + " beginnt " +
Time.newkw(Time.now.kw).to_s
puts “Test: Time.newkw(” + Time.now.kw.to_s + ").kw.to_s = " +
Time.newkw(Time.now.kw).kw.to_s

terminstart = Time.local(2011,05,23,11)
terminende = Time.local(2011,05,23,13,30)

puts "\n\nDer Termin ist in der KW " + terminstart.kw.to_s + " beginnt "

  • terminstart.to_s + " und endet " + terminende.to_s

Hello,

How can I calculate the week dates using a week number ?

EG:
Input (Week Number):
22

Output:
06/12/08 - 06/19/08

Thanks

Bertram S. wrote:

Hi,

Am Mittwoch, 19. Sep 2007, 19:44:24 +0900 schrieb Ilias None:

I’m newbie on Ruby and I like to know how I can manage to print out the
week numbers(counting from monday or sunday).

require “date”
d = Date.today
puts d.cwday, d.cweek, c.cwyear

Bertram

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs