Solved

Determine previous month

Posted on 2015-02-02
10
103 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
  • 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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
This article will show, step by step, how to integrate R code into a R Sweave document
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

809 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