Skip to content

Instantly share code, notes, and snippets.

@Avinash-Bhat
Last active August 29, 2015 14:02
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 Avinash-Bhat/804c465635a27c8a7a7f to your computer and use it in GitHub Desktop.
Save Avinash-Bhat/804c465635a27c8a7a7f to your computer and use it in GitHub Desktop.
Coffeescript to find difference in date
#!/usr/local/bin/coffee
SEC = 1000
MIN = SEC * 60
HOUR = MIN * 60
DAY = HOUR * 24
WEEK = DAY * 7
if process.argv.length != 4
console.log "Insufficient arguments to complete call"
process.exit 1
start = new Date(process.argv[2]).getTime()
end = new Date(process.argv[3]).getTime()
if isNaN(start)
console.log "invalid date: "+ process.argv[2]
process.exit 2
if isNaN(end)
console.log "invalid date: "+ process.argv[3]
process.exit 2
diff = end - start
unit = "millis"
if diff >= WEEK
diff /= WEEK
unit = "weeks"
else if diff >= DAY
diff /= DAY
unit = "days"
else if diff >= HOUR
diff /= HOUR
unit = "hours"
else if diff >= MIN
diff /= MIN
unit = "minutes"
else if diff >= SEC
diff /= SEC
unit = "seconds"
console.log(diff + " " + unit)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment