Skip to content

Instantly share code, notes, and snippets.

@Avinash-Bhat Avinash-Bhat/datediff
Last active Aug 29, 2015

Embed
What would you like to do?
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
You can’t perform that action at this time.