Operating System - HP-UX
1754339 Members
3002 Online
108813 Solutions
New Discussion юеВ

How many days until a certain date?

 
SOLVED
Go to solution
Robert Comber
Advisor

How many days until a certain date?

Hi,

This should be simple but I can't seem to find a command that will work. I need to send out notices like "The system will be down for upgrades in 25 days." I can make a cron entry to call my script but my problem is that I can't find an easy way to calculate the number of days between the current date and some future date like July 4, 2003. This should be so easy but I'm stuck.

Thanks for any help,
Bob
5 REPLIES 5
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: How many days until a certain date?

Hi Bob:

This ain't no hill for a climber.

#!/usr/bin/sh
DATE="7 4 2003"
DAYS=$(($(caljd.sh ${DATE}) - $(caljd.sh)))
echo "Days until ${DATE} = ${DAYS}."

Pretty simple, huh?

If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: How many days until a certain date?

I suppose that we should also add some tests to only print if ${DAYS} >= 0

#!/usr/bin/sh
DATE="7 4 2003"
DAYS=$(($(caljd.sh ${DATE}) - $(caljd.sh)))
if [[ ${DAYS} -ge 0 ]]
then
echo "Days until ${DATE} = ${DAYS}."
fi

That should do it. Caljd.sh -u will give full usage.

If it ain't broke, I can fix that.
Robert Comber
Advisor

Re: How many days until a certain date?

That was fast! I searched for caljd.sh and I got tons of hits.

Thanks,
Bob
Adam J Markiewicz
Trusted Contributor

Re: How many days until a certain date?

Hi Bob

Guess who wrote this script?
I suppose that out dear Albert installed some sort of sniffer that checks the subjects of new problems for words related with dates, time etc.
When the bell is ringing he drops everything and rush to the nearest keyboard. ;)

No points please.
No offense A.

Good luck
Adam
I do everything perfectly, except from my mistakes
Sergejs Svitnevs
Honored Contributor

Re: How many days until a certain date?

and my simple script...

#! /bin/sh
#future date = July 4, 2003 (04-07-2003)
#current date
day=`date +%d`
month=`date +%m`
year=`date +%y`
doj=`date +%j`
new_day=4
new_month=7
i=1
daycount=0
while [ $i -lt $new_month ]
do
monthlength=`cal $i $year|awk '/[0-9]/{last=$NF}END {print last}'`
daycount=`expr $daycount + $monthlength`
i=`expr $i + 1`
done
((daycount+=new_day))
daycount=`expr $daycount - $doj`
echo $daycount" days between the current date and July 4, 2003"

Regards,
Sergejs