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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I''ve done so in the past with the Time class. ((Time.now-past_date)/1.year).round On Nov 20, 10:42 pm, "James Englert" <englert.ja...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
You can also do Time.now.year - other_date.year Cheers, -Roy -----Original Message----- From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org [mailto:rubyonrails-talk@googlegroups.com] On Behalf Of gsterndale Sent: Thursday, November 20, 2008 11:26 PM To: Ruby on Rails: Talk Subject: [Rails] Re: Getting the number of years between two dates I''ve done so in the past with the Time class. ((Time.now-past_date)/1.year).round On Nov 20, 10:42 pm, "James Englert" <englert.ja...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 21 Nov 2008, at 23:26, Jeffrey Pearson wrote:> > 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 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 > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Sat, Nov 22, 2008 at 10:26 AM, Jeffrey Pearson <jeffreywpearson-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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.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 -- http://lindsaar.net/ Rails, RSpec and Life blog.... --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---