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.
8c70506408fddb9740b4de7339055c77?d=identicon&s=25 Prashant Tiwari (tiwaripk)
on 2006-05-08 09: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
77fec2b94c481ad3d2b2135b1f07ef5f?d=identicon&s=25 Marco Lazzeri (Guest)
on 2006-05-08 13:25
(Received via mailing list)
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
8c70506408fddb9740b4de7339055c77?d=identicon&s=25 Prashant Tiwari (tiwaripk)
on 2006-05-08 13: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
77fec2b94c481ad3d2b2135b1f07ef5f?d=identicon&s=25 Marco Lazzeri (Guest)
on 2006-05-08 14:02
(Received via mailing list)
On 08/05/06, Prashant Tiwari <tiwari_p_k@yahoo.com> 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
4005a47a8f2ceee49670b920593c1d52?d=identicon&s=25 Ben Munat (Guest)
on 2006-05-08 17:38
(Received via mailing list)
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
A7736d622f35f8725cc9a0c835d633a3?d=identicon&s=25 Mathias Wittlock (Guest)
on 2006-05-08 17:50
(Received via mailing list)
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.
2299f656aad7dd4e4bde94d90364f09e?d=identicon&s=25 Guest (Guest)
on 2006-05-08 18:14
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.
> 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) %>
4005a47a8f2ceee49670b920593c1d52?d=identicon&s=25 Ben Munat (Guest)
on 2006-05-09 01: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.