Skip to content

Instantly share code, notes, and snippets.

@stranger777
Last active February 7, 2022 07:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save stranger777/d84a6b85c82adbc35cb57430cd5479ae to your computer and use it in GitHub Desktop.
Save stranger777/d84a6b85c82adbc35cb57430cd5479ae to your computer and use it in GitHub Desktop.
Simplest calculator two dates difference. By default in days
#!/bin/bash
#Simplest calculator two dates difference. By default in days
# Usage:
# ./datediff.sh first_date second_date [-(s|m|h|d) | --(seconds|minutes|hours|days)]
first_date=$(date -d "$1" "+%s")
second_date=$(date -d "$2" "+%s")
case "$3" in
"--seconds" | "-s") period=1;;
"--minutes" | "-m") period=60;;
"--hours" | "-h") period=$((60*60));;
"--days" | "-d" | "") period=$((60*60*24));;
esac
datediff=$(( ($first_date - $second_date)/($period) ))
echo $datediff
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment