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


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users How to take the count of all sundays between two dates?
# 1  
Old 12-13-2012
How to take the count of all sundays between two dates?

Hi Am Using Unix Ksh
I have input
Code:
DATE1=01/11/2012
DATE2=10/12/2012

need output as count of all sundays between these two dates

Code:
 for examples Sunday count between DATE1 and DATE2 is 
5

Can anyone help me...

Last edited by Venkatesh1; 12-13-2012 at 01:01 AM..
# 2  
Old 12-13-2012
Is this homework? Also isn't the number of sundays between 11/01 - 12/10 6?
# 3  
Old 12-13-2012
Assuming this is not Homework

Hi

You may try this

Code:
#!/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

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

# 4  
Old 12-13-2012
Thanks maverick, But am getting the Error when i run the script in ksh
Code:
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

# 5  
Old 12-13-2012
Not sure this will work in ksh; also depends heavily on the version of date you use; give it a shot:
Code:
$ 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

# 6  
Old 12-13-2012
Its not working Smilie
# 7  
Old 12-13-2012
It is.
Prove it is not with log/errors/output.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Add previous Sundays date to filename

I am looking for some scripting help. I need to add a time stamp to a file name. I will append data to a file, and want to add to the file name a time stamp of the previous Sundays date. Any takers? (1 Reply)
Discussion started by: sswagner8839
1 Replies

2. UNIX for Advanced & Expert Users

How to get the Missing dates between two dates in the table?

Hi Am Using Unix Ksh ... I have a Table called date select * from date ; Date 01/02/2013 06/02/2013 I need the output as Missing Date 01/02/2013 02/02/2013 03/02/2013 04/02/2013 05/02/2013 06/02/2013 (2 Replies)
Discussion started by: Venkatesh1
2 Replies

3. UNIX for Advanced & Expert Users

How to get the count of sunday between two dates?

Hi Am using unix Ksh I have the two dates DATE1=01/01/2013 DATE2=11/02/2013 In this two dates i need the output as count of sunday sunday=6 Can anyone help me pls!!! (1 Reply)
Discussion started by: Venkatesh1
1 Replies

4. Shell Programming and Scripting

Taking the count of sundays between two date ?

Hi Am using unix Ksh Datecalc and --date functions are not working have two input variables as DATE=01/12/2012 DATE1=23/12/2012 Need output as no of sundays = 4 Can anyone help me pls :( (2 Replies)
Discussion started by: Venkatesh1
2 Replies

5. UNIX for Dummies Questions & Answers

How to write the dates between 2 dates into a file

Hi All, I am trying to print the dates that falls between 2 date variables into a file. Here is the example. $BUS_DATE =20120616 $SUB_DATE=20120613 Output to file abc.txt should be : 20120613,20120614,120120615,20120616 Can you pls help me accomplish this in LINUX. Thanks... (5 Replies)
Discussion started by: dsfreddie
5 Replies

6. UNIX for Dummies Questions & Answers

Listing out sundays alone

I am trying to list out only sundays from december 2011. pandeeswaran@ubuntu:~/training$ cal 12 2011|sed -n '3,$p'|sed -e'/^$/d'|awk '{ORS=",";print $1}' 1,4,11,18,25,,pandeeswaran@ubuntu:~/training$ Can anyone help me? Thanks (13 Replies)
Discussion started by: pandeesh
13 Replies

7. UNIX Desktop Questions & Answers

count of sundays in month

Hi, How to get count of number of sundays in month in unix shell script .. ideally i need 2 get last sunday of month so i used cmd: for eg: for june: cal 06 2011 | tail -2 | head -1 | cut -d" " -f1 hoowever above is wrking for month whose sundays are max=4 but not fr months... (4 Replies)
Discussion started by: musu
4 Replies

8. Shell Programming and Scripting

Need script to generate all the dates in DDMMYY format between 2 dates

Hello friends, I am looking for a script or method that can display all the dates between any 2 given dates. Input: Date 1 290109 Date 2 010209 Output: 300109 310109 Please help me. Thanks. :):confused: (2 Replies)
Discussion started by: frozensmilz
2 Replies

9. Shell Programming and Scripting

Cronjob on alternate sundays

I want to set the crontab job for one of my SIEBEL database to refresh it on alternate sundays. Is there anyway I can do it through cron please ? If not whats the alternative ? :confused: Thanks in advance. (6 Replies)
Discussion started by: abhi123
6 Replies

10. Shell Programming and Scripting

List of Sundays for the years 2000 to 2005

hi, i need to write a script that would list me all sundays in the year 2000 and 2005. output need to be just the date . ;) (1 Reply)
Discussion started by: kquest
1 Replies
Login or Register to Ask a Question