Solved

Determine previous month

Posted on 2015-02-02
10
101 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
 
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

I hope you'll find this tutorial useful and interesting. So let's try to extend Tcl with a new package.  For anyone more deeply interested please check out the book "Practical Programming in Tcl and Tk". It's really one of the best written books abo…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

895 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now