Find one month before date

Hi,

I want two dates one will be the current date and the other one will be just one month before. Say if current month is 11/4/2014 then the other date should be 11/3/2014.

#!/bin/ksh
currentDtae=`date`
oneMonthBefore= ? 

I dont know how to do it. Went through some of the related threads but couldn't find anything clear.

Can anyone please suggest?

Hello Sharma331,

Could you please try following command.

DATE_1_MONTH_BEFORE=`date -d "1 month ago"`
echo $DATE_1_MONTH_BEFORE
Sat Oct 4 04:44:57 EDT 2014

Thanks,
R. Singh

Hi Ravinder,

I have already tried it but it does not recognise the flag d. :frowning:

Hello Sharma331,

Could you please use following. Also as a good practice always let us know the os you are using, it will help us too.

date --date='-1 month'
Sat Oct  4 05:08:29 EDT 2014

Thanks,
R. Singh

can we do something like:

if[current month is january] then
subtract 1 from the year and make the month as 12;
else
subtract one from the month;
fi

Is this logic possible?

---------- Post updated at 04:13 AM ---------- Previous update was at 04:11 AM ----------

This is also not working. :frowning: I don't know why these flags are not recognized.

Hello Sharma331,

Kindly always let us know the Operating system you are using and what you have tried so far to solve the problem, it will always help us too. I am assuming it is solaris, following is the link which may help you in same. Also search option in the forum is very useful you can try the same too.

Thanks,
R. Singh

Hi,

I tried below link. It gives me output but little weared.
http://www.unix.com/shell-programming-and-scripting/124557-how-get-date-n-months-before-unix.html

Actually my requirement is not this complicated. It doesn't bothers me weather the month is of 28 days or 30 days or 31 days.
What i want is if my current date is 2014-02-02 then the date i want is 2014-01-02 which is in format yyyy-mm-dd.
Just want to subtract 1 from the month thats it.

And you don't want to subtract 1 from the year, should the month be January?

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.

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"
1 Like

It looks perfect... Let me try this and get back to you!!

Try your code on the following March 30, 2014. There is no February 30th.

1 Like

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.

2 Likes

Thanks Junior helper!! it works perfectly. :slight_smile:

Hi.

For this specific question, a ksh solution:

printf "%(%b, %B, %m)T\n"  "last month"

producing:

Oct, October, 10

on a system:

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

As "jim mcnamara" noted that this script will give wrong results as months have varying lengths...especially when it is March 30th/31st since February has only 28 or 29 days and IMO you should go for a more robust solution...as simple math wont cut it for date and time problems...

I know you are correct, But the problem you mentioned arises only when your requirement is that you want dates between 1st and last date of every month or you want the report to be generated after 30 days. i.e when you want dates from 1st january to 30th january or say 1st feb to 28th feb. then the calculation becomes complicated and difficult.

What my requirement was i want report to be generated between 2nd January 2014 to 2nd Feb 2014 or lets say 2nd december 2013 to 2nd Jan 2014. It doesnot bothers me whether its a month with 28 days or 30 days or 31 days.

so, this solution works for me.