Skip to content

Instantly share code, notes, and snippets.

@joyrexus
Created December 28, 2013 03:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joyrexus/8155981 to your computer and use it in GitHub Desktop.
Save joyrexus/8155981 to your computer and use it in GitHub Desktop.
Time between two dates

Find the time between two dates in seconds, minutes, etc.

# Find the time between two dates
module.exports = (begin, end) ->
end ?= new Date()
time =
end: end
begin: begin
time.secs = (end - begin) / 1000
time.mins = time.secs / 60
time.hours = time.mins / 60
time.days = time.hours / 24
time.months = time.days / 30
time.years = time.days / 365
time
assert = require 'assert'
duration = require '../duration'
from = new Date("Jan 20, 2009")
to = new Date("Feb 20, 2011")
time = duration(from, to)
assert.ok time.days is 761
assert.ok 2 < time.years < 3
assert.ok 25 < time.months < 26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment