module DateNTime

Extend Date, DateTime and Time

Public Class Methods

end_of_month(date = Date.new(Time.now.year, Time.now.month)) click to toggle source
# File lib/date_n_time.rb, line 123
def self.end_of_month(date = Date.new(Time.now.year, Time.now.month))
  DateTime.end_of_month(date)
end
month_array(start_date, end_date) click to toggle source
# File lib/date_n_time.rb, line 84
  def self.month_array(start_date, end_date)
dates_start = Date.new(start_date.year, start_date.month)
dates_end   = Date.new(end_date.year, end_date.month)+1.month
    
dates_array = []
    
current_date = dates_start
while current_date < dates_end
  sdate = current_date
  edate = current_date+1.month - 1.second

  dates_array << {:start_date => sdate, :end_date => edate}

  current_date = current_date +1.month
end
return dates_array
  end
new_from_params(p) click to toggle source
# File lib/date_n_time.rb, line 5
def self.new_from_params(p)
  return Date.new(p["starts_at(1i)"].to_i, p["starts_at(2i)"].to_i, p["starts_at(3i)"].to_i)
    end
next_month() click to toggle source
# File lib/date_n_time.rb, line 140
def self.next_month
  self.this_month + 1.month
end
prev_months(i=12) click to toggle source
# File lib/date_n_time.rb, line 127
def self.prev_months(i=12)  
  dates = []

  i.times do |x|
    dates << Date.new(Time.now.year, Time.now.month)-(i-x).months
  end
  return dates  
end
this_month() click to toggle source
# File lib/date_n_time.rb, line 136
def self.this_month
  Date.new(Time.now.year, Time.now.month)
end

Public Instance Methods

age_calc() click to toggle source

Return the current age base on a birthdate which is self

# File lib/date_n_time.rb, line 14
  def age_calc
    now = Time.now.to_date
now.year - self.year - (self.change(:year=>now.year)>now ? 1 : 0) #rescue 0
  end
first_of_month() click to toggle source

first day of current month

# File lib/date_n_time.rb, line 119
def first_of_month
  Date.new(self.year, self.month)
end
month_label() click to toggle source
# File lib/date_n_time.rb, line 49
def month_label
  self.strftime('%m/%Y')
end
month_name() click to toggle source
# File lib/date_n_time.rb, line 41
def month_name
  MONTH_TO_VIEW[self.month] rescue self
end
month_name_full() click to toggle source
# File lib/date_n_time.rb, line 45
def month_name_full
  "#{MONTH_TO_VIEW[self.month]} #{self.year}"
end
months_between(date2 = Time.now.to_date, options={:abs => true}) click to toggle source
# File lib/date_n_time.rb, line 102
    def months_between(date2 = Time.now.to_date, options={:abs => true})
  date1 = self
  abs = 1

  if date1.to_date > date2.to_date
    abs = -1 if options[:abs] == false
    recent_date = date1.to_date
    past_date = date2.to_date
  else
    recent_date = date2.to_date
    past_date = date1.to_date
  end

  return ((recent_date.month - past_date.month) + (12 * (recent_date.year - past_date.year))) * abs
end
next_month() click to toggle source

return next month

# File lib/date_n_time.rb, line 20
  def next_month
return self+1.month
  end
prev_month() click to toggle source

return previous month

# File lib/date_n_time.rb, line 25
  def prev_month
return self-1.month
  end
short_date() click to toggle source
# File lib/date_n_time.rb, line 33
def short_date
  self.strftime('%d.%m.')
end
to_de(options = {}) click to toggle source
# File lib/date_n_time.rb, line 29
  def to_de(options = {})
self.strftime('%d.%m.%Y')
  end
to_i() click to toggle source
# File lib/date_n_time.rb, line 53
def to_i
  self.to_s.gsub("-", "").to_i
end
to_short_stamp(options = {}) click to toggle source
# File lib/date_n_time.rb, line 9
def to_short_stamp(options = {})
        return self.strftime('%d%m%y')
end
to_sql() click to toggle source
# File lib/date_n_time.rb, line 37
def to_sql
  self.strftime('%Y-%m-%d')
end
to_zodiac() click to toggle source
# File lib/date_n_time.rb, line 57
  def to_zodiac
    zodiacs = {
      [1,  1,  1,  20] => "Steinbock",
      [1,  21, 2,  19] => "Wassermann",
      [2,  20, 3,  20] => "Fische",
      [3,  21, 4,  20] => "Widder",
      [4,  21, 5,  21] => "Stier",
      [5,  22, 6,  21] => "Zwilling",
      [6,  22, 7,  22] => "Krebs",
      [7,  23, 8,  21] => "Löwe",
      [8,  22, 9,  23] => "Junfrau",
      [9,  24, 10, 23] => "Wage",
      [10, 24, 11, 22] => "Skorpion",
      [11, 23, 12, 22] => "Schütze",
      [12, 23, 12, 31] => "Steinbock"
    }

zodiacs.each do |key, value|
  date1 = Date.new(self.year, key[0], key[1]) 
  date2 = Date.new(self.year, key[2], key[3])
  if self >= date1 and self <= date2
    return value
  end
end

  end