Week number

I

Ilias None

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
 
V

Vellingiri Arul

Ilias said:
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
 
B

Bertram Scharpf

Hi,

Am Mittwoch, 19. Sep 2007, 19:44:24 +0900 schrieb Ilias None:
I=E2=80=99m newbie on Ruby and I like to know how I can manage to print o= ut the
week numbers(counting from monday or sunday).

require "date"
d =3D Date.today
puts d.cwday, d.cweek, c.cwyear

Bertram


--=20
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-scharpf.de
 
I

Ilias Ruby

Thanks Bertram!

Bertram said:
Hi,

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

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

Bertram
 
P

Pr Rm

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
 
S

Siep Korteling

Pr said:
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
 
A

Addis A.

Maybe this is also a goood way :)

With this you can get the weeknumber of a date like

Time.now.kw =3D> 11

or just create the starttime of a week(starting on monday -> for sundy =

set wd to Zero)

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



# F=C3=BCgt der Timeklasse Kalenderwochen beginnend bei Montag ein.
class Time
def kw(wd=3D1)
kw =3D self.strftime("%U").to_i
kw -=3D 1 if self.wday < (wd%7)
return kw
end
def self.newkw(kw=3D0, year=3DTime.now.year.to_i, wd=3D1)
# Gibt Timeobjekt zur=C3=BCck, welches den Start der KW ausgibt
t =3D Time.local(year)
(0..364).each { |n|
if t.kw(wd) =3D=3D kw; break; end
t +=3D 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 =3D " +
Time.newkw(Time.now.kw).kw.to_s

terminstart =3D Time.local(2011,05,23,11)
terminende =3D 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

-- =

Posted via http://www.ruby-forum.com/.=
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,135
Messages
2,570,786
Members
47,342
Latest member
KelseyK737

Latest Threads

Top