Static date adding and subtracting years

Thread Tools Search this Thread
# 1  
Static date adding and subtracting years

Hi Gurus!

I have a static date in a YYYYMMDD format; and I want get the date 2 years in the past and 2 years in the future.

old_date=$static_date - 3 years
future_date=$static_date + 2 years

I was only able to research on dates that are current and not on static dates wherein I can have it set and just changing it when I want.
Please help...
# 2  
old_date=`echo $static_date | awk '{print substr($0,1,4)-3""substr($0,5)}'`
future_date=`echo $static_date | awk '{print substr($0,1,4)+2""substr($0,5)}'`

This User Gave Thanks to bartus11 For This Post:
# 3  
old_date=$((static_date - (3 * 10000)))
future_date=$((static_date + (2 * 10000)))

This User Gave Thanks to balajesuri For This Post:
# 4  
Thanks for the replies... I just wonder if the date is a leap year...
example: 20120229... and there is no 20090229... in this case it should be the next day 20090301 is subtracted by 3 years

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Science: Gadgets
Difficulty: Medium
The Western Electric Model 500 telephone uses tone dialing to dial phone numbers.
True or False?

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 a date based on ls output

I am trying to achieve to get only the month and the day. Example Feb 5 (as you can see if it is feb 1-9) the space is 2. If it is feb 10-28, the space is only 1. I am trying to right a script that will list a directory and shoot an email if there is an activity in last 7 days. I dont really trust... (5 Replies)
Discussion started by: invinzin21
5 Replies

3. Shell Programming and Scripting

awk - Adding and Subtracting Numbers from 2 Columns

Hi Folks, I have a file with 2 columns TAB delimited and I want to add '1' to the first column and subtract '-1' from the second column. What I have tried so far is; awk -F"\t" '{ $1-=1;$2+=1}1' OFS='\t' file File 0623 0623 0624 0624 0643 0643 1059 1037 1037 1037 1038 1038... (2 Replies)
Discussion started by: pshields1984
2 Replies

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

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. Shell Programming and Scripting

Fetch date of 7 years back from current date in Perl

$beginDate = substr(DateCalc("today", "-7Days"),0,8); This fetches the date 7 days back Can I fetch the date before 7 years from todays date in Perl using same syntax Use code tags, see PM. (3 Replies)
Discussion started by: parthmittal2007
3 Replies

7. Windows & DOS: Issues & Discussions

Adding or subtracting days from current date in batch script

Hi, I'm writing an batch file to create report In the batch file iam passing two arguments:startdate and finishdate Ex: startdate=07-sep-2009 finishdate=07-sep-2011 I need to have script that takes command line argument as input and gives me out currentdate last year and current date... (2 Replies)
Discussion started by: anand1773
2 Replies

8. Shell Programming and Scripting

comparing files - adding/subtracting/formating columns

I have two files: file1.txt: FS Total Used Free Used% /u01 10000 8000 2000 80% /u02 10000 8000 2000 80% /u03 10000 8000 2000 80% /u04 10000 8000 2000 80% /u05 10000 8000 2000 80% /u06 10000 8000 2000 80% /u07 10000 8000 2000 80% /u10 10000 5000 5000 50% file2.txt:... (7 Replies)
Discussion started by: oabdalla
7 Replies

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

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

Featured Tech Videos