This little script reports back how many days fall between two arbitrary dates, or one date and today. If you put in a date in the future, it tells how many days as a negative;
# How many days 'till Christmas?
HowManyDays 2017-12-25
-115 |
# How many days 'till Christmas?
HowManyDays 2017-12-25
-115
#!/bin/bash
# SRJ 2017-08-31
date1=$(date --utc --date "${1}" +%s)
Ex=$?
# The line above puts the exit status of the date command
# into the Ex variable, the lines below exit the shell if
# there was an error reported.
if [ ! ${Ex} = 0 ]
then
exit 1
fi
if [ -z "${2}" ]
then
date2=$(date --utc +%s)
else
date2=$(date --utc --date "${2}" +%s)
Ex=$?
# The line above puts the exit status of the date command
# into the Ex variable, the lines below exit the shell if
# there was an error reported.
if [ ! ${Ex} = 0 ]
then
exit 1
fi
fi
diffdays=$(( (date2-date1)/(3600*24) ))
if [ ${diffdays} = 0 ]
then
echo "This script requires a valid date in the form yyyy-mm-dd or, for"
echo "example, HowManyDays 2015-09-23 will report the number of days"
echo "that have passed between 2015-09-23 and today."
echo
echo "If this script is given two dates, it returns the number of days"
echo "between those two dates, if the second date is earlier than the"
echo "first, the result will be a negative number."
echo "HowManyDays 2015-09-23 2016-02-27 will return 157."
exit 1
fi
echo ${diffdays} |
#!/bin/bash
# SRJ 2017-08-31
date1=$(date --utc --date "${1}" +%s)
Ex=$?
# The line above puts the exit status of the date command
# into the Ex variable, the lines below exit the shell if
# there was an error reported.
if [ ! ${Ex} = 0 ]
then
exit 1
fi
if [ -z "${2}" ]
then
date2=$(date --utc +%s)
else
date2=$(date --utc --date "${2}" +%s)
Ex=$?
# The line above puts the exit status of the date command
# into the Ex variable, the lines below exit the shell if
# there was an error reported.
if [ ! ${Ex} = 0 ]
then
exit 1
fi
fi
diffdays=$(( (date2-date1)/(3600*24) ))
if [ ${diffdays} = 0 ]
then
echo "This script requires a valid date in the form yyyy-mm-dd or, for"
echo "example, HowManyDays 2015-09-23 will report the number of days"
echo "that have passed between 2015-09-23 and today."
echo
echo "If this script is given two dates, it returns the number of days"
echo "between those two dates, if the second date is earlier than the"
echo "first, the result will be a negative number."
echo "HowManyDays 2015-09-23 2016-02-27 will return 157."
exit 1
fi
echo ${diffdays}