0

Im trying to show the number of weeks ago calculated from the week number " x weeks ago " this should just increment one week ago, two weeks ago, three weeks ago, 1 month ago, 2 months ago, 1 year ago.. etc

Im using method helper:

  def weeks_ago_in_words(from_time, include_seconds = false)
    to_time   = Time.now
    weeks_ago = ((to_time - from_time)/1.week).abs
    [nil, "last week", "two weeks ago", "three weeks ago"][weeks_ago] ||
        distance_of_time_in_words(from_time, to_time, include_seconds)
  end

In view:

  = weeks_ago_in_words(Time.local(2013) + week.to_i)

week = a week number like 1,10,28,52 etc

This does not give me correct resuls, is there a better way to calculate the "x weeks ago " based on the weeknumer?

Rubytastic
  • 15,001
  • 18
  • 87
  • 175

1 Answers1

0

If you are comparing two dates from the same year here is the code using cweek:

require 'date'

Time.now.to_date.cweek - Date.new(2013,1,1).cweek

If you are comparing two different years you'll have to add some conditional logic to determine your year multiplier as cweek returns the same value for Dec. 31, 2012 and Jan. 1, 2013.

simplystuart
  • 453
  • 2
  • 10