Skip to content

Instantly share code, notes, and snippets.

@StephenOTT
Created May 1, 2014 23:54
Show Gist options
  • Save StephenOTT/93e8f02941e3b23cb156 to your computer and use it in GitHub Desktop.
Save StephenOTT/93e8f02941e3b23cb156 to your computer and use it in GitHub Desktop.
Example of using Chronic gem and TimeDifference gem to get the duration between two statements
require 'chronic'
require 'time_difference'
humanStatement1 = "this tuesday 1pm"
humanStatement2 = "this tuesday 3pm"
humanStatement1Parsed = Chronic.parse(humanStatement1)
humanStatement2Parsed = Chronic.parse(humanStatement2)
# very human readable version
puts TimeDifference.between(humanStatement1Parsed, humanStatement2Parsed).in_hours #=> 2.0
# No need for the Prased Variables version
puts TimeDifference.between(Chronic.parse(humanStatement1), Chronic.parse(humanStatement2)).in_hours #=> 2.0
# Single Line version
puts TimeDifference.between(Chronic.parse("this tuesday 1pm"), Chronic.parse("this tuesday 3pm")).in_hours #=> 2.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment