Skip to content
Gem which calculates the difference between two times
Ruby
Find file
Latest commit 1ff5829 Nov 16, 2013 @abhidsm Update README.rdoc
Failed to load latest commit information.
lib
test
.coveralls.yml Fixed the Gemfile error, coveralls token issue May 1, 2013
.document
.gitignore
.rvmrc Added rvmrc file May 1, 2013
.travis.yml Modified the test script for travis Nov 13, 2012
Gemfile
Gemfile.lock Gemfile compatibility issues are fixed May 1, 2013
LICENSE.txt
README Regenerate gemspec for version 0.3.0 Nov 13, 2012
README.rdoc Update README.rdoc Nov 16, 2013
Rakefile Modified rakefile to add the summry and description Mar 6, 2011
VERSION Version bump to 0.3.0 Nov 13, 2012
time_diff.gemspec Regenerate gemspec for version 0.3.0 Nov 13, 2012

README.rdoc

time_diff

Gem which calculates the difference between two time

Gem Version

Installation

gem install time_diff

Usage

require 'time_diff'

time_diff_components = Time.diff(start_date_time, end_date_time)

This will return the hash of time difference in terms of years, month, week, day, hour, minute and second.

You can use the difference like:

time_diff_components[:year], time_diff_components[:month], time_diff_components[:week]

Formatted Time difference

%y - year

%M - month

%w - week

%d - day

%H - hour

%N - minute

%S - second

%h - hour (without adding 'hour' text to the hours. eg: 3 for 3 hours)

%m - minute (without adding 'minute' text)

%s - second (without adding 'second' text)

By default the format is

'%y, %M, %w, %d and %h:%m:%s'

this will return

'1 year, 2 months, 3 weeks, 4 days and 12:05:52'.

You will get the result from the output hash, time_diff_components[:diff]

You can pass your own format as third parameter to this function.

If you give '%d %h' as the third parameter to the Time.diff() method, then the difference(time_diff_components[:diff]) will be calculated only in days and hours.

Examples

> Time.diff(Time.parse('2011-03-06'), Time.parse('2011-03-07'))
=> {:year => 0, :month => 0, :week => 0, :day => 1, :hour => 0, :minute => 0, :second => 0, :diff => '1 day and 00:00:00'}
> Time.diff(Time.parse('2010-03-06 12:30:00'), Time.parse('2011-03-07 12:30:30'), '%y, %d and %h:%m:%s')
=> {:year => 1, :month => 0, :week => 0, :day => 0, :hour => 18, :minute => 0, :second => 30, :diff => '1 year and 18:00:30'}
> Time.diff(Time.parse('2011-03-06 12:30:00'), Time.parse('2011-03-07 12:30:30'), '%H %N %S')
=> {:year => 0, :month => 0, :week => 0, :day => 1, :hour => 0, :minute => 0, :second => 30, :diff => '24 hours 0 minute 30 seconds'}

i18n support

Add translations for day, days, week, weeks, year, hour, hours, minute, minutes, second, and seconds in your YAML file. For eg:

en:
  day: divasam
  days: divasangal

Contributing to time_diff

  • Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet

  • Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it

  • Fork the project

  • Start a feature/bugfix branch

  • Commit and push until you are happy with your contribution

  • Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.

  • Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.

Copyright

Copyright © 2011 abhidsm. See LICENSE.txt for further details.

Something went wrong with that request. Please try again.