Find one month before date


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find one month before date
# 8  
Old 11-04-2014
And you don't want to subtract 1 from the year, should the month be January?
# 9  
Old 11-04-2014
Sharma331! I just finished a script tailored for the date format required in post #1 ((m)m-(d)d-yyyy)and in the meantime you posted a complete different date format?! Please check if the below code is working for the date format mentioned in post #1, if yes, we can simply adapt it to yyyy-mm-dd

---

Following is a beginner level approach, obviously, but it seems to work and fulfil OPs expectations at 100%. Tested on SunOS' 5.10 date and ksh.

Code:
month=`date '+%m' | tr -d ' '`
day=`date '+%e' | tr -d ' '`
year=`date '+%Y'`

currentDate="$month/$day/$year"

case $month in
    1) year=`expr $year - 1`; month=12
    ;;
    *) month=`expr $month - 1`
    ;;
esac

oneMonthBefore="$month/$day/$year"

This User Gave Thanks to junior-helper For This Post:
# 10  
Old 11-04-2014
Quote:
Originally Posted by junior-helper
Sharma331! I just finished a script tailored for the date format required in post #1 ((m)m-(d)d-yyyy)and in the meantime you posted a complete different date format?! Please check if the below code is working for the date format mentioned in post #1, if yes, we can simply adapt it to yyyy-mm-dd

---

Following is a beginner level approach, obviously, but it seems to work and fulfil OPs expectations at 100%. Tested on SunOS' 5.10 date and ksh.

Code:
month=`date '+%m' | tr -d ' '`
day=`date '+%e' | tr -d ' '`
year=`date '+%Y'`

currentDate="$month/$day/$year"

case $month in
    1) year=`expr $year - 1`; month=12
    ;;
    *) month=`expr $month - 1`
    ;;
esac

oneMonthBefore="$month/$day/$year"

It looks perfect... Let me try this and get back to you!!
# 11  
Old 11-04-2014
Try your code on the following March 30, 2014. There is no February 30th.
This User Gave Thanks to jim mcnamara For This Post:
# 12  
Old 11-04-2014
Because months vary in length 28,29,39, and 31 days finding the previous month means that there have to be some assumptions made by your accounting folks

for example:

Define: last month = "30 days ago". If this operation results in a date in 2 months ago, like March 1 minus 30 days is in January, make a correction. If it results in the first day of the same month, make a correction.

Also this whole date thing is covered in a lot of posts here on the forums by CFA Johnson. He has a website with a really set of bash functions for doing date arithmetic. Perderabo also has datecalc posted on the forums as a sticky (I think it is still there). The Gregorian calendar is messy to work with. IMO, reinventing something that has existing, working code is a bad idea. Writing code to learn about it is great. Just don't use somewhere that has the potential get you fired. Like a payroll application.

Last edited by jim mcnamara; 11-04-2014 at 08:45 AM..
These 2 Users Gave Thanks to jim mcnamara For This Post:
# 13  
Old 11-04-2014
Thanks Junior helper!! it works perfectly. Smilie
# 14  
Old 11-04-2014
Hi.

For this specific question, a ksh solution:
Code:
printf "%(%b, %B, %m)T\n"  "last month"

producing:
Code:
Oct, October, 10

on a system:
Code:
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian 5.0.8 (lenny, workstation) 
ksh 93s+

See also dateutils

Best wishes ... cheers, drl
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How bash treats literal date value and retrieve year, month and date?

Hi, I am trying to add few (say 3 days) to sysdate using - date -d '+ 3 days' +%y%m%d and it works as expected. But how to add few (say 3 days) to a literal date value and how bash treats a literal value as a date. Can we say just like in ORACLE TO_DATE that my given literal date value... (2 Replies)
Discussion started by: pointers1234
2 Replies

2. Shell Programming and Scripting

How to find all files other than first two dates & last date per month and year?

how to find all files other than first two dates & last date per month and year Hi All, lets say there are following files in directory -rwxr-xr-x 1 user userg 1596 Mar 19 2012 a.txt -rwxr-xr-x 1 user userg 1596 Mar 19 2012 b.txt -rwxr-xr-x 1 user userg ... (6 Replies)
Discussion started by: Makarand Dodmis
6 Replies

3. UNIX for Advanced & Expert Users

Find all files other than first two files dates & last file date for month

Hi All, I need to find all files other than first two files dates & last file date for month and month/year wise list. lets say there are following files in directory Mar 19 2012 c.txt Mar 19 2012 cc.txt Mar 21 2012 d.txt Mar 22 2012 f.txt Mar 24 2012 h.txt Mar 25 2012 w.txt Feb 12... (16 Replies)
Discussion started by: Makarand Dodmis
16 Replies

4. Red Hat

How to find/display out last Friday's date of the month?

Hello, Can you please help me find/display out last Friday's date of the month using command in Unix/Linux (3 Replies)
Discussion started by: sunnysthakur
3 Replies

5. Shell Programming and Scripting

Help with getting last date of previous month and first date of previous 4th month from current date

I have requirment to get last date of previous month and the first date of previous 4th month: Example: Current date: 20130320 (yyyymmdd) Last date of previous month: 20130228 (yyyymmdd) First date of previous 4th month: 20121101 (yyyymmdd) In my shell --date, -d, -v switches are not... (3 Replies)
Discussion started by: machomaddy
3 Replies

6. UNIX for Dummies Questions & Answers

print previous month (current month minus 1) with Solaris date and ksh

Hi folks month=`date +%m`gives current month Howto print previous month (current month minus 1) with Solaris date and ksh (7 Replies)
Discussion started by: slashdotweenie
7 Replies

7. Shell Programming and Scripting

Pass the first date and last date of previous month

Hi All, I need to run a job every month at the beginning of the month which is scheduled through autosys, lets say on 03/01/2010. I need to pass the last month's i.e February's first_date = 02/01/2010 and last_date = 02/28/2010 as variables to a stored procedure. Can somebody please pass... (2 Replies)
Discussion started by: vigdmab
2 Replies

8. Shell Programming and Scripting

How to find the create time of a file if current date is in next month

Hi All, I want to find the time diffrence between currnt time and "abc.txt" file create time. I have solve that but if the abc.txt file created last month then is there any process to find the difftent? Exp: Create time of abc.txt is "Apr 14 06:48" and currect date is "May 17 23:47".... (1 Reply)
Discussion started by: priyankak
1 Replies

9. Shell Programming and Scripting

Use date command to find last month

#!/usr/bin/ksh Does anyone have a good way to set a variable to last month? For example, today is 20070810. I would like to use the date command to set a variable to last months %m code, which is 07. If I pluck this months value (08) and user expr to do simple math on it, it returns 7 (not... (5 Replies)
Discussion started by: Cbish68
5 Replies

10. UNIX for Dummies Questions & Answers

find out month from a date

I would like to find out the month from a given date, how is it possible. (5 Replies)
Discussion started by: rudoraj
5 Replies
Login or Register to Ask a Question