How do you manipulate dates? I take a string x = "2/4/2007".to_date then I want to add 5 days to it returning "2/9/2007" as a date I could just pull it apart and add the days to it, but if it should switch the month then it becomes a problem. I saw things like 1.week.ago but that deals with the current date, and x.1.week.ago returns an error. Thanks for any help. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Try making it a Time object instead of a Date:>> t = Time.mktime(2007,2,4)=> Sun Feb 04 00:00:00 CST 2007>> t.ago(2.days)=> Fri Feb 02 00:00:00 CST 2007>> 5.days.since(t)=> Fri Feb 09 00:00:00 CST 2007 You can call to_date on the Time object if you actually need a Date object finally. ed On 2/8/07, Alex Treber <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > How do you manipulate dates? > > I take a string x = "2/4/2007".to_date > > then I want to add 5 days to it returning "2/9/2007" as a date > > I could just pull it apart and add the days to it, but if it should > switch the month then it becomes a problem. > > I saw things like 1.week.ago but that deals with the current date, and > x.1.week.ago returns an error. > > Thanks for any help. > > -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Alex Treber wrote:> How do you manipulate dates? > > I take a string x = "2/4/2007".to_date > > then I want to add 5 days to it returning "2/9/2007" as a date > > I could just pull it apart and add the days to it, but if it should > switch the month then it becomes a problem. > > I saw things like 1.week.ago but that deals with the current date, and > x.1.week.ago returns an error. > > Thanks for any help.x = x.advance(:days => 5) -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
James Byrne wrote: should be: x = x.to_time.advance(:days => 5) -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
James Byrne wrote:> James Byrne wrote: > should be: > > x = x.to_time.advance(:days => 5)and to return a date object x = x.to_time.advance(:days => 5).to_date -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
check this : http://errtheblog.com/post/44 x = "2/4/2007".to_time y = 1.week.ago(x) z = 2.days.since(x) Charly On 8 fév, 15:59, Alex Treber <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> How do you manipulate dates? > > I take a string x = "2/4/2007".to_date > > then I want to add 5 days to it returning "2/9/2007" as a date > > I could just pull it apart and add the days to it, but if it should > switch the month then it becomes a problem. > > I saw things like 1.week.ago but that deals with the current date, and > x.1.week.ago returns an error. > > Thanks for any help. > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ 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 guys rock! Thanks for the help! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---