Subtracting A month from an arbitrary date in UNIX


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Subtracting A month from an arbitrary date in UNIX
# 1  
Old 10-08-2013
Subtracting A month from an arbitrary date in UNIX

Hi,

Is there a way to subtract a month or at least 30 days from an arbitrary or user inputted date without the GNU date?

Example:

please Input date: 2011-05-11

then the answer will be 2011-04-11

This code doesn't work:
Code:
$date -d "2011-05-11 - 1 month" "+%Y%m%d"

Thanks

Last edited by erin00; 10-08-2013 at 02:20 AM..
# 2  
Old 10-08-2013
Doest work will not be much of help...
What output do you get ? ( error message or output...)
What shell are you using and what OS?

That said
Code:
date -d "2011-05-11 - 1 month" "+%Y%m%d"

Will not give your suspected output for it is not what are asking for ( your last format is missing the minus signs...)
it should be
Code:
date -d "2011-05-11 - 1 month" "+%Y-%m-%d"

# 3  
Old 10-08-2013
sorry about the minus sign(-), I just forgot about it.

the error I am receiving is this:

Code:
date: Not a recognized flag: d
Usage: date [-u] [+"Field Descriptors"]


Last edited by Scott; 10-09-2013 at 01:56 AM.. Reason: Code tags
# 4  
Old 10-09-2013
You have not mentionned your shell or most important your OS... it seems that -d is a GNU stuf... so supported by linux, I doubt other OS
Code:
#linux:
has:/home/vbe $ date -d "2011-05-11 - 1 month" "+%Y-%m-%d"
2011-04-11

#    -  BUT --- 
#solaris:
nm1:/export/home/vbe $ uname -r
5.10
nm1:/export/home/vbe $ date -d "2011-05-11 - 1 month" "+%Y-%m-%d"
date: illegal option -- d
usage:  date [-u] mmddHHMM[[cc]yy][.SS]
        date [-u] [+format]
        date -a [-]sss[.fff]
#HP-UX:
ran:/home/vbe $ uname -r
B.11.11
ran:/home/vbe $ date -d "2011-05-11 - 1 month" "+%Y-%m-%d"
date: illegal option -- d
Usage: date [-u] [+format]
       date [-u] [mmddhhmm[[cc]yy]]
       date [-a [-]sss.fff]
#AIX:
n12:/home/vbe $ oslevel
7.1.0.0
n12:/home/vbe $ date -d "2011-05-11 - 1 month" "+%Y-%m-%d"
date: Not a recognized flag: d
Usage: date [-u] [+"Field Descriptors"]

# 5  
Old 10-09-2013
my OS is AIX, I am using korn shell and my perl is v5.12.4

Last edited by erin00; 10-09-2013 at 08:53 PM..
# 6  
Old 10-10-2013
If it's just that - reducing month by one with carry - try this:
Code:
X=2011-05-11
Y=$((${X:5:3}1)); Z=${X:0:4}; [ $Y -le 0 ] && { Y=12; Z=$((Z-1)); }; printf "%s-%02d-%s\n" $Z ${Y} ${X:8}
2011-04-11

# 7  
Old 10-10-2013
I tried it in Cygwin and it worked fine. and this is what I really wanted to happen.

But when I tried it on the putty I got this error:

ksh: ${X:5:3}1: 0403-011 The specified substitution is not valid for this command.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Changing CSV files with date . Subtracting date by values

Hi All, I have a CSV file which is as below. Basically I need to take the year column in it and find if the year is >= 20152 . If that is then I should subtract all values by 6. In the below example in description I am having number mentioned as YYWW so I need to subtract those by -5. Whereever... (8 Replies)
Discussion started by: arunkumar_mca
8 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

Adding/ Subtracting from Date

Hi , How can I add/substruct x number of days with date? For example My_Date=`date` Now I need Hope it's clear. (2 Replies)
Discussion started by: Anupam_Halder
2 Replies

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

5. Shell Programming and Scripting

SQL one liner for subtracting 1 from date

I got a statement like below to subtract 1 from given date using teradata. I am looking for a one line unix command to perform the same. select 'parse_this_record', (DATE '${FILE_DATE}' - 1) (FORMAT 'YYYY-MM-DD'); Input: 2012-02-21 Expected Output: 2012-02-20 PS: One liner because I am... (2 Replies)
Discussion started by: siteregsam
2 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. UNIX for Dummies Questions & Answers

adding or subtracting days in the o/p of date

how can we add or subtract days from the output of date command in unix... like if i want to subtract a day from the result of date command like this.. v_date=`date +%Y%m%d` this wud give me 20080519 now i want to subtract one day from this.. so tht it wud give me 20080518.. how do i do... (1 Reply)
Discussion started by: St.Fartatric
1 Replies

8. UNIX for Dummies Questions & Answers

How do I set my date in a unix script to 'new month

Hi, I'm a newbie to unix and i'm loving every bit of it. I need assistance in setting my date function in my script to "new month". What I want to do is define the "newmonth" as the 1st of every month so my script can run by this time evry month through the year. Can someone assist. ... (1 Reply)
Discussion started by: Agent704
1 Replies

9. Shell Programming and Scripting

Subtracting date / timestamps

I have looked through the forums and found many date / time manipulation tools, but cannot seem to find something that fits my needs for the following. I have a log file with date time stamps like this: Jun 21 17:21:52 Jun 21 17:24:56 Jun 21 17:27:59 Jun 21 17:31:03 Jun 21 17:34:07 Jun... (0 Replies)
Discussion started by: roadcyclist
0 Replies

10. Shell Programming and Scripting

how to get month last date in unix

Hello I want to get end date of the any month in unix . is there any function to do this. we can get month from date command 'date +%b' example: If Feb month then last date is 28 if sep then last date 30. like this thanks please help (12 Replies)
Discussion started by: rajan_ka1
12 Replies
Login or Register to Ask a Question