Prashant Tiwari
2006-May-08 07:22 UTC
[Rails] How to get difference between two dates in days???
Hi, I want to calculate difference between Current date & previous date (stored in database). The difference should be in days. Hhow to do that?? PLs tell me. Thanx in advance. Prash -- Posted via http://www.ruby-forum.com/.
Marco Lazzeri
2006-May-08 11:25 UTC
[Rails] Re: How to get difference between two dates in days???
Prashant Tiwari <tiwari_p_k@...> writes:> I want to calculate difference between Current date & previous date > (stored in database). The difference should be in days. Hhow to do > that??What about: $ irb> require ''date'' > ( Date.new(2006, 01, 02) - Date.new(2006, 01, 01) ).to_i=> 1 ? Regards, Marco
Prashant Tiwari
2006-May-08 11:48 UTC
[Rails] Re: How to get difference between two dates in days???
HI, but when I am trying following into my view it giving me error:- ==========( Date.new(2006, 01, 02) - Date.new(2006, 01, 01) ).to_i =========== & error is:- ===========compile error ./script/../config/../app/views/comments/show.rhtml:74: syntax error Date.new(2006, 01, 02) - Date.new(2006, 01, 01) ); _erbout.concat "\n" ^ ============Why I am getting this error?> Thanx, Prash -- Posted via http://www.ruby-forum.com/.
Marco Lazzeri
2006-May-08 12:01 UTC
[Rails] Re: How to get difference between two dates in days???
On 08/05/06, Prashant Tiwari <tiwari_p_k@yahoo.com> wrote:> HI, > but when I am trying following into my view it giving me error:- > ==========> ( Date.new(2006, 01, 02) - Date.new(2006, 01, 01) ).to_i > ===========> > & error is:- > ===========> compile error > ./script/../config/../app/views/comments/show.rhtml:74: syntax error > Date.new(2006, 01, 02) - Date.new(2006, 01, 01) ); _erbout.concat "\n" > ^ > ============> Why I am getting this error?>That was a pure Ruby snippet of code. If you wish to calculate the difference between two dates in a View, you have to write something like: <%= ( @my_model.previous_date - Date.today() ).to_i %> (assuming you''ve your model in an instance variable called ''my_model''). Marco
Ben Munat
2006-May-08 15:35 UTC
[Rails] Re: How to get difference between two dates in days???
Marco Lazzeri wrote:> That was a pure Ruby snippet of code. > > If you wish to calculate the difference between two dates in a View, > you have to write something like: > > <%= ( @my_model.previous_date - Date.today() ).to_i %> > > (assuming you''ve your model in an instance variable called ''my_model'').I''m a little rusty, but doesn''t erb want strings? Seems like I had to put a .to_s on the end of stuff like the above. b
Mathias Wittlock
2006-May-08 15:49 UTC
[Rails] Re: How to get difference between two dates in days???
On 5/8/06, Ben Munat <bent@munat.com> wrote:> Marco Lazzeri wrote: > I''m a little rusty, but doesn''t erb want strings? Seems like I had to put a .to_s on the > end of stuff like the above.Well, my Rails doesn''t complain when I do <%= "50".to_i %> at least. (I just tried it). :) Mathias.
Guest
2006-May-08 16:14 UTC
[Rails] Re: How to get difference between two dates in days???
Prashant Tiwari wrote:> Hi, > > I want to calculate difference between Current date & previous date > (stored in database). The difference should be in days. Hhow to do > that?? > PLs tell me. > > Thanx in advance. > PrashHave a look at the Rails API for the method distance_of_time_in_words example: - <%= distance_of_time_in_words(Time.now, record.created_on) %> -- Posted via http://www.ruby-forum.com/.
Ben Munat
2006-May-08 23:16 UTC
[Rails] Re: How to get difference between two dates in days???
Huh. I don''t know what I was thinking of then... sorry for the noise. b Mathias Wittlock wrote:> On 5/8/06, Ben Munat <bent@munat.com> wrote: > >> Marco Lazzeri wrote: >> I''m a little rusty, but doesn''t erb want strings? Seems like I had to >> put a .to_s on the >> end of stuff like the above. > > > Well, my Rails doesn''t complain when I do <%= "50".to_i %> at least. > (I just tried it). :) > > Mathias. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails