Solved

Determine previous month

Posted on 2015-02-02
10
105 Views
Last Modified: 2015-02-10
I am trying to determine the previous month.  For example if it is Feb. 1, I would need files with a month of 01 (which is within the file name).  I can get the current month , however, when I try to subtract 1 it gives me 1 instead of 01.  Is there a better way to do this?


year=$(date +"%y")
month=$(date +"%m")
let prevmonth=10#$month-1
echo $prevmonth
0
Comment
Question by:bje
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
  • 2
  • +1
10 Comments
 
LVL 68

Assisted Solution

by:woolmilkporc
woolmilkporc earned 214 total points
ID: 40584806
If you're on Linux (means: if you have GNU date) try this:

prevmonth=$(date -d "$(date +%Y-%m-15) -1 month" "+%m")
0
 
LVL 20

Assisted Solution

by:jmcg
jmcg earned 144 total points
ID: 40584821
Have you thought about what you want to happen if the current date is in January? In that case, the year needs to be adjusted as well.

Can I assume you're using the GNU date command? Or one that follows a similar syntax?

year=$(date --date="1 month ago" +"%y")
prevmonth=$(date --date="1 month ago" +"%m")

echo $prevmonth $year
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 40585135
@jmcg: Your solution will not work on March, 29 (except leap years), March, 30 and 31, May, 31, July, 31, October, 31 and December, 31.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 20

Accepted Solution

by:
jmcg earned 144 total points
ID: 40585362
You are correct, woolmilkporc.

I wondered about that "15" in your answer (we must have been writing answers at the same time and yours posted first). I went and did some more digging and found:

http://www.gnu.org/software/tar/manual/html_chapter/tar_7.html

where it says:

The fuzz in units can cause problems with relative items. For example, `2003-07-31 -1 month' might evaluate to 2003-07-01, because 2003-06-31 is an invalid date. To determine the previous month more reliably, you can ask for the month before the 15th of the current month. For example:
$ date -R
Thu, 31 Jul 2003 13:02:39 -0700
$ date --date='-1 month' +'Last month was %B?'
Last month was July?
$ date --date="$(date +%Y-%m-15) -1 month" +'Last month was %B!'
Last month was June!

Open in new window


Also, take care when manipulating dates around clock changes such as daylight saving leaps. In a few cases these have added or subtracted as much as 24 hours from the clock, so it is often wise to adopt universal time by setting the TZ environment variable to `UTC0' before embarking on calendrical calculations.

So the "15" is somewhat arbitrary and could just as well be any number from "1" to "28", right?

Excel has an EOMONTH function which is frequently referred to for finding previous month in a spreadsheet context.

The Perl module Date::Manip, which I've used in the past, manages to avoid this problem in most cases by truncating the day-of-the-month to the last day of the month when calculating deltas that include a non-zero month.

So what looks like a simple question can really get quite deep (though not nearly as deep as deciding the date of Easter)!
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 40585753
>> So the "15" is somewhat arbitrary and could just as well be any number from "1" to "28", right? <<

Right!
0
 
LVL 68

Assisted Solution

by:woolmilkporc
woolmilkporc earned 214 total points
ID: 40585990
If you don't have GNU date a simple awk can do:

prevmonth=`awk -v M=$(date +%m) 'BEGIN {PM=M-1; if(PM==0) PM=12; printf "%02s\n",PM}'`

or better (if your shell supports it):

prevmonth=$(awk -v M=$(date +%m) 'BEGIN {PM=M-1; if(PM==0) PM=12; printf "%02s\n",PM}')
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 142 total points
ID: 40586031
prevmonth=$(printf %02d $((($(date +%m)+10)%12+1)))
Do you also need the year corresponding to the prevmonth
0
 

Author Comment

by:bje
ID: 40586475
Yes, would also need the year.
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 142 total points
ID: 40586577
YM=`date +%Y%m`
prevmonth=$(printf %02d $(((#10${YM:4}+10)%12+1)))
prevmonthyear=$(( ${YM:0:4}-10#$prevmonth/12 ))
echo $prevmonthyear-$prevmonth
0
 
LVL 68

Assisted Solution

by:woolmilkporc
woolmilkporc earned 214 total points
ID: 40586753
GNU date:

prevmonth_year=$(date -d "$(date +%Y-%m-15) -1 month" "+%m_%Y")

2-digit year:

prevmonth_year=$(date -d "$(date +%Y-%m-15) -1 month" "+%m_%y")

awk:

prevmonth_year=$(awk -v M=$(date +%m) -v Y=$(date +%Y) 'BEGIN {PM=M-1; if(PM==0) {PM=12; Y--}; printf "%02s_%4s\n",PM, Y}')

2-digit year:

prevmonth_year=$(awk -v M=$(date +%m) -v Y=$(date +%y) 'BEGIN {PM=M-1; if(PM==0) {PM=12; Y=(Y+99)%100}; printf "%02s_%02s\n",PM, Y}')
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question