How to generate a specific Date on Ruby

on 2011/10/29 - -


somocode.rb
require 'date'

# Today
Date.today

# 1st of this month
Date.new(Time.now.year, Time.now.month, 1)

# 1st of next month
Date.new(Time.now.year, Time.now.month, 1) >> 1

# 1st of last month
Date.new(Time.now.year, Time.now.month, 1) << 1

# 1 week later
Date.today + 7

# 1 week before
Date.today - 7



And you can use these Date valuables in Model 'where' phrase on Rails.


somocode.rb
models = SomeModel.where("date <= ?", Date.today)


No comments :