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/.=