Display month for Previous day


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Display month for Previous day
# 1  
Old 02-01-2009
Display month for Previous day

Hello - I have one question regarding the date. I wanted to display the month name for previous day. The output should be as follows...

5-Feb-09 => February
1-Feb-09 => January
28-Feb-09=> February

Here is the code i am using to get the output....

date '+%m %d %Y' |
{
read MONTH DAY YEAR
DAY=`expr "$DAY" - 1`
case "$DAY" in
0)
MONTH=`expr "$MONTH" - 1`
case "$MONTH" in
0)
MONTH=12
YEAR=`expr "$YEAR" - 1`
;;
esac
DAY=`cal $MONTH $YEAR | grep . | fmt -1 | tail -1`
esac
}


Based on this code, here is the output.

5-Feb-09 => February(it is correct)
1-Feb-09 => February(it is not correct.. It should display January).
28-Feb-09=> February(it is correct)
# 2  
Old 02-01-2009
On FreeBSD :
Code:
# date
Sun Feb  1 11:32:11 EST 2009
# date +%B
February
# date -v -1d
Sat Jan 31 11:32:28 EST 2009
# date -v -1d +%B
January

# 3  
Old 02-01-2009

With GNU date:

Code:
date -d -1day +%B

In any POSIX shell:

Code:
months="December November October September August July June May April March February January December "
eval "$( date "$@" "+month=%B day=%d" )"
day=$(( $day - 1 ))
if [ $day -eq 0 ]
then
  month=${months#*$month }
  month=${month%% *}
fi
printf "%s\n" "$month"

# 4  
Old 02-01-2009
Quote:
Originally Posted by cfajohnson
[indent]
With GNU date:

Code:
date -d -1day +%B

Same in "plain english" Smilie
Code:
date -d "1 day ago" +%B

# 5  
Old 02-01-2009
Quote:
Originally Posted by danmero
Same in "plain english" Smilie
Code:
date -d "1 day ago" +%B


Too prolix! Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Move the files between Current day & a previous day

Hi All, I have a requirement where I need to first capture the current day & move all the files from a particular directory based on a previous day. i.e move all the files from one directory to another based on current day & a previous day. Here is what I am trying, but it gives me errors.... (2 Replies)
Discussion started by: dsfreddie
2 Replies

2. Programming

How to get First and Last day of previous month from trunc(sysdate)

Hi Guys, How to get First day of previous month and last day of previous month from trunc(sysdate) using SQL..? Thanks in advance. Br, pinpe (2 Replies)
Discussion started by: pinpe
2 Replies

3. Shell Programming and Scripting

Script to counting a specific word in a logfile on each day of this month, last month etc

Hello All, I am trying to come up with a shell script to count a specific word in a logfile on each day of this month, last month and the month before. I need to produce this report and email it to customer. Any ideas would be appreciated! (5 Replies)
Discussion started by: pnara2
5 Replies

4. Shell Programming and Scripting

Code creates day 32 instead of 1st day of next month.

I am using the code below modified from a post I saw here regarding having the script write out future dates. The problem is that instead of making 8/1 it makes 7/32! Please help! yy=`date +%Y` mm=`date +%m` dd=`date +%d` echo "Today is : $yy $mm $dd" #!/usr/bin/ksh date '+%m... (5 Replies)
Discussion started by: libertyforall
5 Replies

5. UNIX for Dummies Questions & Answers

print previous month (current month minus 1) with Solaris date and ksh

Hi folks month=`date +%m`gives current month Howto print previous month (current month minus 1) with Solaris date and ksh (7 Replies)
Discussion started by: slashdotweenie
7 Replies

6. Shell Programming and Scripting

Script to find previous month last day minus one day timestamp

Hi All, I need to find the previous month last day minus one day, using shell script. Can you guys help me to do this. My Requirment is as below: Input for me will be 2000909(YYYYMM) I need the previous months last day minus 1 day timestamp. That is i need 2000908 months last day minus ... (3 Replies)
Discussion started by: girish.raos
3 Replies

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

8. Shell Programming and Scripting

last working day of previous month

Hi, I want a script(ksh) to see if today is the last working day(Mon-Fri) of the month. If it is the last working day I need to print current date, else I need the last working day of previous month. Thanks in advance. (1 Reply)
Discussion started by: rspk_praveen
1 Replies

9. Shell Programming and Scripting

How to find the first day of previous month in unix?

How to find the first day of previous month in unix mmddyyyy format? example : today is 07052007 (in mmddyyyy) output sud be 06012007 thanks mohapatra (10 Replies)
Discussion started by: mohapatra
10 Replies

10. Shell Programming and Scripting

Write a shell script to find whether the first day of the month is a working day

Hi , I am relatively new to unix... Can u pls help me out to find out if the first day of the month is a working day ie from (Monday to Friday)...using Date and If clause in Korn shell.. This is very urgent. Thanks for ur help... (7 Replies)
Discussion started by: phani
7 Replies
Login or Register to Ask a Question