Getting the number of years between two dates

Hey, I need to get the number of years (as a number) between two dates. Here is what I have as a helper.

Returns the number of years between now and the specified date.

def years_ago(date) dateDifference = DateTime.now - date results =Date.day_fraction_to_time(dateDifference) return results[0] / 24 / 365; end

I’m sure there is a better way to do this. What is it?

Thanks, Jim

I've done so in the past with the Time class.

((Time.now-past_date)/1.year).round

You can also do

  Time.now.year - other_date.year

Cheers,

-Roy

The thread about the dates got me kicking myself for not posting to
ask. The website I manage for my kids school that WAS running on php/ joomla got hacked and defaced.....so I am taking the opportunity to
learn RonR to set up a new site for them.

On the front page, I need to display the schedule of activities for
the current week. I am having issues figuring out how to get the dates
for the current week (with two seperate definitions: the first is
Sunday through Saturday, the other being Monday through Friday).

I have spent hours pouring over:

The Ruby on Rails Bible and Ruby on Rails Unleashed

books as well as google but still hitting road blocks.

Can someone help me out and describe to me how to:

1) Figure out the dates 2) Display them on the web page. I dont want this stored in the db so
I dont want to add it to my model.

Any assistance would be GREATLY appreciated.

Jeff PEarson

On the front page, I need to display the schedule of activities for the current week. I am having issues figuring out how to get the dates for the current week (with two seperate definitions: the first is Sunday through Saturday, the other being Monday through Friday).

This isn't really a rails question so I'm not that surprised its not
in the books you've listed. Rails has beginning_of_week, however that is one specific definition
of week. If you have a look inside it's easy to adapt that.

You just need to look at Time.now.wday, which varies from 0 (on a
sunday) to 6 (on a saturday).

So if your weeks start on thursday then you're looking for the
previous/next date with a wday of 4. You can do this naively by just
adding a day at a time until some_date.wday has the right value or you
can work out the increments yourself.

it's just arithmetic modulo 7. if d is the number of days until the
next thursday, and n is Time.now.wday then it must be that

n+d = 4 [7] so d = 4-n [7] ie d = 4-n +k*7 for an appropriate value of k.

So days until thursdays are given by d=4-n + k*7.

If you want the next thursday then that imposes 7>=d >0 so if 0 <= n <4 (ie today is sunday through wednesday) then k = 0, so
d = 4 - Time.now.wday if n>= 4 (thursday through saturday) then k = 1 so d = 11 -
Time.now.wday

and similarly for finding the previous thursday (this is probably an
overcomplicated way of reasoning about this - showing my background
here)

Fred

I had this same problem.

Try something like this:

Create a file in config/initializers called ‘time.rb’

In there put something like this:

class Time

def Time.last_monday

today = Time.now

case

when today.wday == 1

days_since_monday = 7

when today.wday > 1

days_since_monday = today.wday - 1

when today.wday == 0

days_since_monday = 6

end

today - days_since_monday.day

end

end

Now, once you restart your app, from anywhere you can call Time.last_monday and get a time object with the right date.

Then you can do:

Time.last_monday + 1.day #=> Tue

Time.last_monday + 2.days #=> Wed

Time.last_monday + 3.days #=> Thu

Time.last_monday + 4.days #=> Fri

Time.last_monday + 5.days #=> Sat

Time.last_monday + 6.days #=> Sun

I’ll leave modifying the class to you to make Time.last_sunday

The way I figured this out was looking in the Rails source code in ActiveSupport’s time extensions

HTH, Mikel