Monday, February 26, 2007

ruby date time parse

require 'parsedate'
res = ParseDate.parsedate("2000-01-01")
stamp = Time.local(*res).to_i
for i in 0 .. 365
time_stamp = stamp + 24 * 60 * 60 * i
t = Time.at(time_stamp)
actual_date = t.strftime("%Y-%m-%d")
day_of_week = t.strftime("%w").to_i + 1
day_of_month = t.strftime("%d").to_i
day_of_year = t.strftime("%j").to_i
month_of_year = t.strftime("%m").to_i
day_name = t.strftime("%A")
day_short_name = t.strftime("%a")
month_name = t.strftime("%B")
month_short_name = t.strftime("%b")
week_of_year = t.strftime("%W")
actual_year = t.strftime("%Y")

# quarter_of_year
quarter_of_year = (month_of_year - 1) / 3 + 1

# week_of_month
interval = 6
start_date = 0
end_date = day_of_week + 1
for n in 0 .. 5
if day_of_month >= start_date && day_of_month < end_date week_of_month = n + 1 break end start_date = end_date end_date = (n + 1) * interval + day_of_week + 1 end puts actual_date puts week_of_month puts quarter_of_year end


SELECT WEEK('1998-12-31',1);#mysql week() function

No comments :