Skip to content

Instantly share code, notes, and snippets.

@michaelrepper
Created February 10, 2015 07:35
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 michaelrepper/38087fb2784174f5b6c7 to your computer and use it in GitHub Desktop.
Save michaelrepper/38087fb2784174f5b6c7 to your computer and use it in GitHub Desktop.
r/Dailyprogrammer Challenge #201 Easy
##################################################
# r/Dailyprogrammer Challenge #201 Easy #
# coded by Michael Repper #
# query [a * t ] gridoodle [ d * o * t ] com #
##################################################
#####################################################################################
# Takes 1 or 2 dates as parameters. The dates should strings be in the format: #
# "YYYY-mm-dd" "YYYY-mm-dd" OR "YYYY-mm-dd" #
# If only one date is supplied, the days are calculated from the current date. #
# If two days are supplied, it calculates the days between them. #
#####################################################################################
function days_from ()
{
if [ $# -gt 1 ]
then
date_1=($1)
date_2=($2)
else
date_1=`date +%Y-%m-%d`
date_2=($1)
fi
result=$(echo $((($(date -d $date_1 +'%s') - $(date -d $date_2 +'%s'))/60/60/24)))
if [[ $result -lt 0 ]]
then
((result*=-1))
fi
echo $result "days from" $date_1 "to" $date_2
}
days_from "2012-01-01" "2019-10-12"
days_from "2020-01-01"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment