How to find second and fourth Monday of the month?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to find second and fourth Monday of the month?
# 8  
Old 06-30-2015
Looks like your shell doesn't zero pad the MONTH sequence as does my bash. You may want to consider the various options that your shell offers for "brace expansion". Otherwise, prepend single digit MONTHs with a "0" by shell means.

---------- Post updated at 09:40 ---------- Previous update was at 09:39 ----------

or, use seq -w 1 12.
This User Gave Thanks to RudiC For This Post:
# 9  
Old 06-30-2015
Are you trying to determine if the script is started on the correct day, i.e. 2nd & 4th Monday only?

If so, start it every day and have a section to test the date at the top similar to this:-
Code:
date '+%d %w'|read dayofmonth dayofweek
((weekofmonth=1+$dayofmonth/7))

if [ $dayofweek -ne 1 -o \( $weekofmonth -ne 2 -a $weekofmonth -ne 4 \) ]
then
   printf "Not running today.\n"
   exit 0
fi

# Now able to run main script

Apologies if I have missed the point, but I hope that this helps.

Robin
This User Gave Thanks to rbatte1 For This Post:
# 10  
Old 06-30-2015
Quote:
Originally Posted by rbatte1
Are you trying to determine if the script is started on the correct day, i.e. 2nd & 4th Monday only?

If so, start it every day and have a section to test the date at the top similar to this:-
Code:
date '+%d %w'|read dayofmonth dayofweek
((weekofmonth=1+$dayofmonth/7))

if [ $dayofweek -ne 1 -o \( $weekofmonth -ne 2 -a $weekofmonth -ne 4 \) ]
then
   printf "Not running today.\n"
   exit 0
fi

# Now able to run main script

Apologies if I have missed the point, but I hope that this helps.

Robin
Thanks Robin. That's what the intention is.

Could you please advise why you are adding 1 in the below code:
Code:
((weekofmonth=1+$dayofmonth/7))

Is it related to day of the week? Or It will be same irrespective of whether the month starts on Mon,Tue,Wed,Thu,Fri,Sat or Sun?

---------- Post updated at 07:48 PM ---------- Previous update was at 07:45 PM ----------

Quote:
Originally Posted by RudiC
Looks like your shell doesn't zero pad the MONTH sequence as does my bash. You may want to consider the various options that your shell offers for "brace expansion". Otherwise, prepend single digit MONTHs with a "0" by shell means.

---------- Post updated at 09:40 ---------- Previous update was at 09:39 ----------

or, use seq -w 1 12.
Thanks RudiC.
It is working now.

Code:
$ for MONTH in `seq -w 1 12`
>     do printf "%2s.%2s.%4s      %2s.%2s.%4s\n" \
>                 $(($(date -d"2015${MONTH}01" +"16 - %u - (%u==1?7:0)"))) $MONTH 2015 \
>                 $(($(date -d"2015${MONTH}01" +"30 - %u - (%u==1?7:0)"))) $MONTH 2015
> done
12.01.2015      26.01.2015
 9.02.2015      23.02.2015
 9.03.2015      23.03.2015
13.04.2015      27.04.2015
11.05.2015      25.05.2015
 8.06.2015      22.06.2015
13.07.2015      27.07.2015
10.08.2015      24.08.2015
14.09.2015      28.09.2015
12.10.2015      26.10.2015
 9.11.2015      23.11.2015
14.12.2015      28.12.2015
$

I have also managed to do the same using below code.
Code:
$ for MONTH in `seq -w 1 12`
> do
> cal $MONTH $YEAR | tail -n +3 | cut -c4-5 | tr -s '\n' ','  | sed 's/[ \t]*//g' | awk -F "," -v M=$MONTH -v Y=$YEAR '{if($1>7 || $1==""){printf "%2s.%2s.%4s\t%2s.%2s.%4s\n",$3,M,Y,$5,M,Y} else {printf "%2s.%2s.%4s\t%2s.%2s.%4s\n",$2,M,Y,$4,M,Y}}'
> done
12.01.2015      26.01.2015
 9.02.2015      23.02.2015
 9.03.2015      23.03.2015
13.04.2015      27.04.2015
11.05.2015      25.05.2015
 8.06.2015      22.06.2015
13.07.2015      27.07.2015
10.08.2015      24.08.2015
14.09.2015      28.09.2015
12.10.2015      26.10.2015
 9.11.2015      23.11.2015
14.12.2015      28.12.2015
$

Let me know if this code is fine.

Last edited by Prathmesh; 06-30-2015 at 11:21 AM.. Reason: Added my code
# 11  
Old 06-30-2015
For my part, in the line:-
Code:
((weekofmonth=1+$dayofmonth/7))

... the logic is that I divide the day of the month by seven (losing any remainder) and that gives me a value between zero and four. I add the 1 to make it easier to understand when testing, i.e. 1 for first week, 2 for second week etc.

That's all,
Robin
This User Gave Thanks to rbatte1 For This Post:
# 12  
Old 06-30-2015
Quote:
Originally Posted by rbatte1
For my part, in the line:-
Code:
((weekofmonth=1+$dayofmonth/7))

... the logic is that I divide the day of the month by seven (losing any remainder) and that gives me a value between zero and four. I add the 1 to make it easier to understand when testing, i.e. 1 for first week, 2 for second week etc.

That's all,
Robin
Ok. Understood, Thanks.
# 13  
Old 06-30-2015
ksh93 have builtin support for such date queries

For example:
Code:
$ printf "%T\n"   "second monday june"
Mon Jun  8 00:00:00 EDT 2015
$ printf "%T\n"   "fourth monday june"
Mon Jun 22 00:00:00 EDT 2015

You can format the output anyway you like.

For example:
Code:
$ printf "%(%D)T\n"   "fourth monday june"
06/22/15

These 2 Users Gave Thanks to fpmurphy For This Post:
# 14  
Old 06-30-2015
Quote:
Originally Posted by fpmurphy
ksh93 have builtin support for such date queries

For example:
Code:
$ printf "%T\n"   "second monday june"
Mon Jun  8 00:00:00 EDT 2015
$ printf "%T\n"   "fourth monday june"
Mon Jun 22 00:00:00 EDT 2015

You can format the output anyway you like.

For example:
Code:
$ printf "%(%D)T\n"   "fourth monday june"
06/22/15

Thanks. Although my shell does not support it. It is good thing to know.

---------- Post updated at 09:26 PM ---------- Previous update was at 09:19 PM ----------

Quote:
Originally Posted by rbatte1
For my part, in the line:-
Code:
((weekofmonth=1+$dayofmonth/7))

... the logic is that I divide the day of the month by seven (losing any remainder) and that gives me a value between zero and four. I add the 1 to make it easier to understand when testing, i.e. 1 for first week, 2 for second week etc.

That's all,
Robin
Hi Robin,

I had modified your code and tried to check for one year. It is showing some discrepancies.

Can you please advise here.

Code:
$ cat Monday_Date.sh
#!/bin/bash

YEAR=2015
for MONTH in `seq -w 1 12`
do
        for DATE in `seq -w 1 31`
        do
                dayofmonth=`date -d"${YEAR}${MONTH}${DATE}" +%d`
                dayofweek=`date -d"${YEAR}${MONTH}${DATE}" +%w`
                ((weekofmonth=1+$dayofmonth/7))

                if [ $dayofweek -ne 1 -o \( $weekofmonth -ne 2 -a $weekofmonth -ne 4 \) ]
                then
                        #printf "Not running today.\n"
                        continue
                else
                        MONDAY_DATE=`date -d"${YEAR}${MONTH}${DATE}" +%d.%m.%Y`
                        echo "$MONDAY_DATE"
                fi
        done
done

Output:
Code:
$ sh Monday_Date.sh 2>/dev/null
12.01.2015
26.01.2015
09.02.2015
23.02.2015



09.03.2015
23.03.2015
13.04.2015
27.04.2015

11.05.2015
25.05.2015
08.06.2015
22.06.2015

13.07.2015
27.07.2015
10.08.2015
24.08.2015
07.09.2015
21.09.2015

12.10.2015
26.10.2015
09.11.2015
23.11.2015

07.12.2015
21.12.2015
$

Second and Fourth Monday dates for December and September seems to be wrong.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find one month before date

Hi, I want two dates one will be the current date and the other one will be just one month before. Say if current month is 11/4/2014 then the other date should be 11/3/2014. #!/bin/ksh currentDtae=`date` oneMonthBefore= ? I dont know how to do it. Went through some of the related threads... (15 Replies)
Discussion started by: Sharma331
15 Replies

2. UNIX for Advanced & Expert Users

How to find last two files for a month?

Hi All, I need to find last two files for the month. lets say there are following files in directory -rwxr-xr-x 1 user userg 1596 Mar 19 15:43 c.txt -rwxr-xr-x 1 user userg 1596 Mar 21 15:43 d.txt -rwxr-xr-x 1 user userg 1596 Mar 22 15:43 f.txt -rwxr-xr-x 1... (14 Replies)
Discussion started by: Makarand Dodmis
14 Replies

3. HP-UX

How to find a file created in UNIX every monday.???

Hi All Any one please suggest me... I have one directory every monday one file will be created in that directory. so if the file is created on monday or not i need check first. How can write a script??? if the file is not created i want to quit from script. Thanks K.Srinivas (5 Replies)
Discussion started by: k_s_rao7
5 Replies

4. UNIX for Advanced & Expert Users

Crontab For First Monday Of Every Month!!

Hi, Could any one please let me know the crontab entry for scheduling a job for every first monday of the month? Thank You in advance, Sue (2 Replies)
Discussion started by: pyaranoid
2 Replies

5. UNIX for Dummies Questions & Answers

cron script -run every 2nd day of month except Monday

I know I can't schedule this in cron and would have to write a wrapper around my script and schedule it in cron ....but not sure how do to this? How do I exclude Monday if the 2nd day of the month falls on a Monday? Thanks. I tried this: 0 0 2 * 0,2-6 command And I know this doesnt... (2 Replies)
Discussion started by: newtou
2 Replies

6. UNIX for Dummies Questions & Answers

I wanted to get the date of the first monday of a month.

Hi, I need to display the date of the first monday of a month. Can any one please help me on this. Thanks in advance. (6 Replies)
Discussion started by: Sheethal
6 Replies

7. UNIX for Dummies Questions & Answers

find out month from a date

I would like to find out the month from a given date, how is it possible. (5 Replies)
Discussion started by: rudoraj
5 Replies

8. Shell Programming and Scripting

how can i find the third friday of each month?

Help please! I need to read the calendar and put the date of the third Friday of each month into a variable for comparison in an "if" statement. How would I do this? Thnx, leslie02 (10 Replies)
Discussion started by: leslie02
10 Replies

9. Shell Programming and Scripting

Find all files by month

Hi, I am trying to do achieving of files by months. find /test -name \*.* -mtime +30 will give me the result of all modified files after 30 days. But lets say i want to list all files that is modified in last months... what is the command to do it? Thanks! (13 Replies)
Discussion started by: maldini
13 Replies

10. UNIX for Dummies Questions & Answers

Crontab First Monday of Month only

Is there a way to setup a cronjob that will only run on the first monday of the month? (3 Replies)
Discussion started by: molonede
3 Replies
Login or Register to Ask a Question

Featured Tech Videos