Solved

Determine previous month

Posted on 2015-02-02
10
100 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

758 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

17 Experts available now in Live!

Get 1:1 Help Now