Sponsored Content
Top Forums UNIX for Advanced & Expert Users How to take the count of all sundays between two dates? Post 302743603 by maverick_here on Thursday 13th of December 2012 04:34:35 AM
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

 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
DATEPERIOD.__CONSTRUCT(3)						 1						 DATEPERIOD.__CONSTRUCT(3)

DatePeriod::__construct - Creates a new DatePeriod object

SYNOPSIS
public DatePeriod::__construct (DateTimeInterface $start, DateInterval $interval, int $recurrences, [int $options]) DESCRIPTION
DatePeriod::__construct (DateTimeInterface $start, DateInterval $interval, DateTimeInterface $end, [int $options]) DatePeriod::__con- struct (string $isostr, [int $options]) Creates a new DatePeriod object. PARAMETERS
o $start - The start date of the period. o $interval - The interval between recurrences within the period. o $recurrences - The number of recurrences. o $end - The end date of the period. o $isostr - An ISO 8601 repeating interval specification. o $options - Can be set to DatePeriod::EXCLUDE_START_DATE to exclude the start date from the set of recurring dates within the period. CHANGELOG
+--------+---------------------------------------------------+ |Version | | | | | | | Description | | | | +--------+---------------------------------------------------+ | 5.5.8 | | | | | | | $end type changed to DateTimeImmutable. Previ- | | | ously, DateTime. | | | | | 5.5.0 | | | | | | | $start type changed to DateTimeImmutable. Previ- | | | ously, DateTime. | | | | +--------+---------------------------------------------------+ EXAMPLES
Example #1 DatePeriod example <?php $start = new DateTime('2012-07-01'); $interval = new DateInterval('P7D'); $end = new DateTime('2012-07-31'); $recurrences = 4; $iso = 'R4/2012-07-01T00:00:00Z/P7D'; // All of these periods are equivalent. $period = new DatePeriod($start, $interval, $recurrences); $period = new DatePeriod($start, $interval, $end); $period = new DatePeriod($iso); // By iterating over the DatePeriod object, all of the // recurring dates within that period are printed. foreach ($period as $date) { echo $date->format('Y-m-d')." "; } ?> The above example will output: 2012-07-01 2012-07-08 2012-07-15 2012-07-22 2012-07-29 Example #2 DatePeriod example with DatePeriod::EXCLUDE_START_DATE <?php $start = new DateTime('2012-07-01'); $interval = new DateInterval('P7D'); $end = new DateTime('2012-07-31'); $period = new DatePeriod($start, $interval, $end, DatePeriod::EXCLUDE_START_DATE); // By iterating over the DatePeriod object, all of the // recurring dates within that period are printed. // Note that, in this case, 2012-07-01 is not printed. foreach ($period as $date) { echo $date->format('Y-m-d')." "; } ?> The above example will output: 2012-07-08 2012-07-15 2012-07-22 2012-07-29 PHP Documentation Group DATEPERIOD.__CONSTRUCT(3)
All times are GMT -4. The time now is 01:25 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy