Forum: Ruby on Rails How to get difference between two dates in days???

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Prashant T. (Guest)
on 2006-05-08 11:22
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
Marco L. (Guest)
on 2006-05-08 15:25
(Received via mailing list)
Prashant T. <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 T. (Guest)
on 2006-05-08 15:48
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
Marco L. (Guest)
on 2006-05-08 16:02
(Received via mailing list)
On 08/05/06, Prashant T. <removed_email_address@domain.invalid> wrote:
> 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 M. (Guest)
on 2006-05-08 19:38
(Received via mailing list)
Marco L. 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 W. (Guest)
on 2006-05-08 19:50
(Received via mailing list)
On 5/8/06, Ben M. <removed_email_address@domain.invalid> wrote:
> Marco L. 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 (Guest)
on 2006-05-08 20:14
Prashant T. 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.
> Prash

Have 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) %>
Ben M. (Guest)
on 2006-05-09 03:17
(Received via mailing list)
Huh. I don't know what I was thinking of then... sorry for the noise.

b
This topic is locked and can not be replied to.