Dear all Is there any existing function in rails to calculate time? I have google it but no information found.. For example a = "2009-07-16 12:34".to_time b = "2009-07-16 12:04".to_time some_method(a,b) return "30 mins ago" c = "2009-07-16 12:34".to_time d = "2009-07-12 09:09".to_time some_method(c,d) return "4 days ago" If not, I will write it by my own. But it can save my time if there is existing method to do so... Many thanks Valentino -- Posted via http://www.ruby-forum.com/.
2009/7/16 Valentino Lun <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>:> > Dear all > > Is there any existing function in rails to calculate time? I have google > it but no information found.. > > For example > a = "2009-07-16 12:34".to_time > b = "2009-07-16 12:04".to_time > some_method(a,b) return "30 mins ago" > > c = "2009-07-16 12:34".to_time > d = "2009-07-12 09:09".to_time > some_method(c,d) return "4 days ago" > > If not, I will write it by my own. But it can save my time if there is > existing method to do so... >Have a look at the documentation for the Time class. You can just subtract the times.>> a = "2009-07-16 12:34".to_time=> Thu Jul 16 12:34:00 UTC 2009>> b = "2009-07-16 12:04".to_time=> Thu Jul 16 12:04:00 UTC 2009>> diff = a-b=> 1800.0 # 30 mins in seconds Colin
You also can use the DateHelper (http://api.rubyonrails.org/classes/ ActionView/Helpers/DateHelper.html). Always the Rails API is the best place to start. Regards. Franco Catena. On Jul 16, 1:22 am, Valentino Lun <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Dear all > > Is there any existing function in rails to calculate time? I have google > it but no information found.. > > For example > a = "2009-07-16 12:34".to_time > b = "2009-07-16 12:04".to_time > some_method(a,b) return "30 mins ago" > > c = "2009-07-16 12:34".to_time > d = "2009-07-12 09:09".to_time > some_method(c,d) return "4 days ago" > > If not, I will write it by my own. But it can save my time if there is > existing method to do so... > > Many thanks > Valentino > -- > Posted viahttp://www.ruby-forum.com/.