How to take the count of all sundays between two dates?

Hi Am Using Unix Ksh
I have input

DATE1=01/11/2012
DATE2=10/12/2012

need output as count of all sundays between these two dates

 for examples Sunday count between DATE1 and DATE2 is 
5

Can anyone help me...

Is this homework? Also isn't the number of sundays between 11/01 - 12/10 6?

Hi

You may try this

#!/bin/ksh

DATE1="$1"
DATE2="$2"
TMP1=`mktemp`

NDATE1=`date +%s -d "$DATE1"`
NDATE2=`date +%s -d "$DATE2"`

DIFFD=`echo "$NDATE2 - $NDATE1" |bc`

NUMDAY=`echo "$DIFFD / ( 60 * 60 * 24 )" | bc`
for ((i=1; i <= NUMDAY ; i = i + 1))
do
date --date="$i day" |grep  "^Sun" >> $TMP1
done

echo "Number of Sundays between $1 and $2 : `cat $TMP1|wc -l`"
rm -rf $TMP1

The catch is that your date input should and should only be in the following format YYYY-MM-DD . If you want it to be anything else write a small wrapper in the beginning to convert the input format to YYYY-MM-DD

Usage

[root@#####]# sh dow.sh 2012-11-01 2012-12-10
Number of Sundays between 2012-11-01 and 2012-12-10 : 6


Thanks maverick, But am getting the Error when i run the script in ksh

The following error :-
Invalid character in date/time specification.
Usage: date [-u] [+Field Descriptors]
Invalid character in date/time specification.
Usage: date [-u] [+Field Descriptors]
syntax error on line 1 stdin
syntax error on line 1 stdin
user.sh[13]: syntax error at line 13 : `(' unexpected
 

Not sure this will work in ksh ; also depends heavily on the version of date you use; give it a shot:

$ A=$(date +%j -d"11/01/12")
$ B=$(date +%j -d"12/10/12")
$ C=$(date +%u -d"11/01/12")
$ echo $(( (B-A)/7 + ((B-A)%7+C)/7 ))
6

Its not working :frowning:

It is.
Prove it is not with log/errors/output.

Getting error :-

Invalid character in date/time specification.
Usage: date [-u] [+Field Descriptors]
Invalid character in date/time specification.
Usage: date [-u] [+Field Descriptors]
 

So your version of date does not support the solution I proposed. Tough luck.

What is you OS ( type and Version ) also the date version

Check this thread Subtract days to a date in AIX 5.3 Post: 302736539

Since there was no reply to the Question "Is this homework" And the way the thread owner replies, I assume it is so thread closed!
Dear forumers when asked the question, dont assume it isnt and reply, please...
We dont want to see any replies while subsist doubts, thanks a lot

1 Like