Find one month before date


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find one month before date
# 1  
Old 11-04-2014
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.
Code:
#!/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?
# 2  
Old 11-04-2014
Hello Sharma331,

Could you please try following command.

Code:
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
# 3  
Old 11-04-2014
Hi Ravinder,

I have already tried it but it does not recognise the flag d. Smilie
# 4  
Old 11-04-2014
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.

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

Thanks,
R. Singh
# 5  
Old 11-04-2014
can we do something like:
Code:
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 ----------

Quote:
Originally Posted by RavinderSingh13
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.

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

Thanks,
R. Singh
This is also not working. Smilie I don't know why these flags are not recognized.
# 6  
Old 11-04-2014
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.

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


Thanks,
R. Singh
# 7  
Old 11-04-2014
Hi,

I tried below link. It gives me output but little weared.
How to get date n months before in Unix?

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.
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