How to find/display out last Friday's date of the month?


 
Thread Tools Search this Thread
Operating Systems Linux Red Hat How to find/display out last Friday's date of the month?
# 1  
Old 03-21-2013
How to find/display out last Friday's date of the month?

Hello,

Can you please help me find/display out last Friday's date of the month using command in Unix/Linux
# 2  
Old 03-21-2013
Dear sunnysthakur,

I'm hoping I have understood the request correctly, so I will have a go with a ksh script.
Code:
#!/bin/ksh

day_of_week=`date +%w`          # Sunday is zero
day_of_month=`date +%d`         # Current day of month
year=`date +%Y`                 # Four digit year
month=`date +%m`                # Current month


((delta=$day_of_week+2))

if [ $delta -ge 8 ]             # Is this Friday or Saturday
then
   ((delta=$delta-7))           # Correct for Friday/Saturday
fi

((friday_dom=$day_of_month-$delta))


if [ "$friday_dom" -le 0 ]      # Is this a valid value?
then
   ((month=$month-1))           # Previous month
   if [ $month -eq 0 ]
   then
      month=12
      ((year=$year-1))
   fi
   last_week=`cal $month $year |tail -2|head -1`
   last_day_prev_month="${last_week##* }"
   ((friday_dom=$last_day_prev_month+$friday_dom))    # Actually a subtraction
fi

print "Last Friday was ${friday_dom}, month $month and year $year."

You will need to check that the manual page for date agrees with the options at the top.

There is probably a neat way in perl to convert the date into seconds and calculate there before returning to a date format.


I hope that this helps and the logic I have used is clear.


Robin
Liverpool/Blackburn
UK
# 3  
Old 03-21-2013
The ksh93 built-in printf has %T formatting option:
Code:
#!/bin/ksh93

printf "%(%D)T\n" "last friday in Mar 2013"

From ksh93 manual:
Code:
A %(date-format)T  format can be use to treat an argument as a date/time string and to format the date/time according 
to the date-format as defined for the date(1) command.

This User Gave Thanks to Yoda For This Post:
# 4  
Old 03-29-2013
This also works for me.

cal 03 2013 | awk 'NR==1 {m=substr($1, 1, 3); y=$2} NF>5 {d=$6} END {print "Friday", m, d, y}'
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Linux

Bash Display First Friday of the next month

Hello, I need to find the date of next first Friday of the month and set as a variable in a bash script ie - FIRSTFRIDAY=$(date -dfirst-friday +%d) I know date -dfirst-friday doesn't work, but unsure if I can use this / cal + awk or something else to find the right date of the... (7 Replies)
Discussion started by: summerdays
7 Replies

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

3. UNIX for Dummies Questions & Answers

[Solved] Cron - job to run every 3rd Friday of the month only

Hi Expert Please help me to set a cron job schedule, Ihave a job that run every 3rd Friday of the month at 1030am. I tried to set up like this, but the job still runs every friday at 1030am. I want the job to run every 3rd Friday of the month at 1030am only 30 10 15,16,17,18,19,20,21... (2 Replies)
Discussion started by: kaibiganmi
2 Replies

4. Shell Programming and Scripting

cron job to run on second to last friday each month

I needed a cron job to run on the second to last friday of every month. Our servers are running HP-UX, and the HP-UX date command is pretty basic and does not have all of the fancy options that Linux date command does, and it does not have the ability at all to return future dates. So I had to... (0 Replies)
Discussion started by: lupin..the..3rd
0 Replies

5. Shell Programming and Scripting

needs to display month for previous day date

Hello, I wanted to display the month for previous day date. Like, today date is 18-Nov-2008. So the previous date is 17-Nov-2008. The output should be November. If the today date is 1-DEC-2008, then output should be NOVEMBER. If the today date is 1-JAN-2008, then output should be DECEMBER.... (4 Replies)
Discussion started by: govindts
4 Replies

6. Shell Programming and Scripting

Last friday of every month

Hi, I need to get the date of last friday of every month. how can i achieve this ? please guide me. Thanks in advance (3 Replies)
Discussion started by: apsprabhu
3 Replies

7. Shell Programming and Scripting

Use date command to find last month

#!/usr/bin/ksh Does anyone have a good way to set a variable to last month? For example, today is 20070810. I would like to use the date command to set a variable to last months %m code, which is 07. If I pluck this months value (08) and user expr to do simple math on it, it returns 7 (not... (5 Replies)
Discussion started by: Cbish68
5 Replies

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

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

10. Shell Programming and Scripting

Need help, Every friday in a month

I am trying to write a script that shows every Friday in a month. I used cal $1 $2 | grep -v "^$" | awk '{print $6}' It doesn't work for the frist week of Friday because calendar command output has some spaces in the first line and awk '{print $6}' doesn't work. Anybody help me with this... (3 Replies)
Discussion started by: LAY
3 Replies
Login or Register to Ask a Question