How to get date n months before in Unix?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to get date n months before in Unix?
# 1  
Old 11-25-2009
How to get date n months before in Unix?

Hi,

I need the date n months before currrent date
I need to store date in a varible say x
and then get the date 6 month before and get the result in the format 25-Aug-2009

Please Advice
# 2  
Old 11-25-2009
If you have GNU date you can do something like:

Code:
date -d "2 month ago" +%F

otherwise you can check one of the links of:

https://www.unix.com/answers-frequent...rithmetic.html
# 3  
Old 11-25-2009
Hi Franklin,

I have tried with the above command and the article too doesn't help in terms of finding the date n month before the current date. i am using AIX unix and the commands as mentioned for Free bSD and gnu dates aren't working.
# 4  
Old 11-25-2009
Maybe there are shorter solutions with perl but with awk you can do something like this in your script:

Code:
x=11

newdate=$(echo $x | awk -v date="$(date "+%d %m %Y")" -v OFS="-" '
{ 
  num=$0
  split(date, a); d=a[1]; m=a[2]; y=a[3]
  m-=num
  while(m < 1) {m+=12; y--}
  printf("%02d%s%02d%s%s\n", d, OFS, m, OFS, y)
}')

echo $newdate

You can change the format in the printf statement, the variable OFS is the separator.
Play around with it to get the desired format.
# 5  
Old 11-26-2009
There is a problem with the end of month. You would need to add some code so that the function doesn't return invalid dates like "31-Nov-2009".
# 6  
Old 11-26-2009
Thanks a lot Franklin this is working fine!!

---------- Post updated at 05:49 AM ---------- Previous update was at 05:48 AM ----------

Ygor thanks for the input i am working on adding that check
# 7  
Old 11-26-2009
Quote:
Originally Posted by Ygor
There is a problem with the end of month. You would need to add some code so that the function doesn't return invalid dates like "31-Nov-2009".
Oops, right, it's much more complicated than that Smilie but here we go:

Code:
x=11
newdate=$(echo $x | 
awk -v date="$(date "+%d %m %Y")" -v OFS="-"  '
{ 
  num=$0
  split(date, a); d=a[1]; m=a[2]; y=a[3]
  m-=num
  while(m < 1) {m+=12; y--}
  if(m == 2 && d >= 29){
    if((y % 4 == 0 && y % 100 != 0) || (y % 400 == 0)){
      d-=29
      m++
    }
    else {
      d-=28
      m++
    }
  }
  if(d == 31 && (m < 8 && m % 2 == 0) || m > 7 && m % 2 ) {
    d=1
    m++
  }  
  if(m > 12)y++
  while(m < 1) {m+=12; y--}
  printf("%02d%s%02d%s%s\n", d, OFS, m, OFS, y)
}')

echo $newdate

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. HP-UX

awk command in hp UNIX subtract 30 days automatically from current date without date illegal option

current date command runs well awk -v t="$(date +%Y-%m-%d)" -F "'" '$1 < t' myname.dat subtract 30 days fails awk -v t="$(date --date="-30days" +%Y-%m-%d)" -F "'" '$1 < t' myname.dat awk command in hp unix subtract 30 days automatically from current date without date illegal option error... (20 Replies)
Discussion started by: kmarcus
20 Replies

2. Shell Programming and Scripting

Subtracting months from passed Date

Hi All, I am getting a date from environment variable and want to do some processing by subtracting 2 months from the date passed through the environment variable. I am trying the following syntax : date_var=2014-08-31 date_2M_ago='$date_var+"%d%m%y" --$date_var="2 months ago" '... (3 Replies)
Discussion started by: Rahul Raj
3 Replies

3. Shell Programming and Scripting

Subtract months/days from date

Hi, Can you please let me know code for the below (in korn shell) a) Subtract month(s) from given date b) Subtract day(s) from give date c) Subtract month(s) from given timestamp d) Subtract day(s) from give timestamp (1 Reply)
Discussion started by: tostay2003
1 Replies

4. HP-UX

Actual date minus six months (HP UX)

Hi there, is it possible to get the actual date minux six months with just a simple command? It's easy with Linux but on HP Unix (for me) impossible ;) Best wishes (3 Replies)
Discussion started by: System
3 Replies

5. Shell Programming and Scripting

current date - 8 months in perl script

I have a requirement as follows. when i pass a date to the perl script, it has to calculate the current date - 8 months and output the date back to the shell script in date format (YYYY-MM-DD). Current date - 8 months is not constant.. because leap year, and the months jan, mar, may,.... has... (4 Replies)
Discussion started by: kmanivan82
4 Replies

6. UNIX and Linux Applications

sqlite: calculating with dates - compare current date minus 6 months with stored record

Hi I have a table with name, date in format DD.MM.YYYY. I need to something like this (I try to explain in pseudo code) if SYSDATE (current date) minus 6 months > $expiry date print OK else print NOK with $name and $expiry date I know this is possible with Oracle. How to do this... (0 Replies)
Discussion started by: slashdotweenie
0 Replies

7. Shell Programming and Scripting

Compare date from db2 table to yesterday's Unix system date

I am currently running the following Korn shell script which works fine: #!/usr/bin/ksh count=`db2 -x "select count(*) from schema.tablename"` echo "count" I would like to add a "where" clause to the 2nd line that would allow me to get a record count of all the records from schema.tablename... (9 Replies)
Discussion started by: sasaliasim
9 Replies

8. Shell Programming and Scripting

Delete files older than 3 months.(read date from the name of the file)

Guys, My log files stored in the date format format below(log_20080714072942): TIMESTAMP=`date +%Y%m%d%H%M%S` LOG=/log/log_${TIMESTAMP}.log I'm looking for a shell script which deletes all files which is older than 3 months from today. Regards, Bhagat (3 Replies)
Discussion started by: bhagat.singh-j
3 Replies

9. Shell Programming and Scripting

Cron to run first day of month to calculate date 3 months ago

Hi, I would like to find out how can i calculate a date which is 3 months ago. I intend to run a cron job on the 1st of every month, and calculate the month 4 months earlier from the date. For example, if today's date is 1st May 2007, i would like to return 012007( January 2007). i can get... (1 Reply)
Discussion started by: new2ss
1 Replies

10. UNIX for Advanced & Expert Users

Subtract 2 months from the date

I have the script which appends month and year to the name of the file. Now every time when I append the month-year combination I have to subtract 2 months from the current date and then append it, since we are sending our vendor 2 months prior worth of data eveytime. #! /usr/bin/ksh ... (5 Replies)
Discussion started by: mahekr2000
5 Replies
Login or Register to Ask a Question