Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Determine previous month

Posted on 2015-02-02
10
Medium Priority
?
108 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 856 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 576 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
Technology Partners: 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 576 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 856 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 568 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 568 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 856 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

610 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