Abhi On Rails

Ruby On Rails - What I learned recently

Updated time_diff gem

Now with time_diff gem you can calculate the time differnce between two Time in the formatted form by passing a third parameter to the Time.diff() method.

If you are not passing any parameter then by default it will take the third parameter value as'%y, %M, %w, %d and %h:%m:%s' and it will return the formatted time difference in the form: '1 year, 2 months, 3 weeks, 4 days and 12:05:52'.

For hours, minutes and seconds have two formats, with text and without text. The parameter is with '%h', '%m', '%s' are the formats which won't add text with the value, ie., it return 3 for '3 hours'. The parameters with he formats '%H', '%N', '%S' will return the values with text, ie., '2 hours', '30 minutes'.

If you are using the default format ie., formats with comma seperated, then it will remove the intervals(year, month, ..) which are zero.

If you need only day and hours in the time difference you can specify that by passing parameter in the format of '%d %h'. This will calculate the formatted time diff text only for days and hours.

blog comments powered by Disqus
acts_as_taggable_on gem hav... →
← Create ruby gem using Jeweler