Last active
August 29, 2015 14:02
-
-
Save Avinash-Bhat/804c465635a27c8a7a7f to your computer and use it in GitHub Desktop.
Coffeescript to find difference in date
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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