106

I have Linux ( RH 5.3) machine

I need to add/calculate 10 days plus date so then I will get new date (expiration date))

for example

 # date 
 Sun Sep 11 07:59:16 IST 2012

So I need to get

     NEW_expration_DATE = Sun Sep 21 07:59:16 IST 2012

Please advice how to calculate the new expiration date ( with bash , ksh , or manipulate date command ?)

migrated from serverfault.com Sep 25 '12 at 0:17

This question came from our site for system and network administrators.

147

You can just use the -d switch and provide a date to be calculated

date
Sun Sep 23 08:19:56 BST 2012
NEW_expration_DATE=$(date -d "+10 days")
echo $NEW_expration_DATE
Wed Oct 3 08:12:33 BST 2012 
  -d, --date=STRING
          display time described by STRING, not ‘now’

This is quite a powerful tool as you can do things like

date -d "Sun Sep 11 07:59:16 IST 2012+10 days"
Fri Sep 21 03:29:16 BST 2012

or

TZ=IST date -d "Sun Sep 11 07:59:16 IST 2012+10 days"
Fri Sep 21 07:59:16 IST 2012

or

prog_end_date=`date '+%C%y%m%d' -d "$end_date+10 days"`

So if $end_date=20131001 then $prog_end_date=20131011

  • 2
    Thank you!! Your examples should be in the GNU date info page! – Davor Cubranic Nov 15 '17 at 13:30
  • Never knew I could do this - that is a really nice solution. – Tim Seed Apr 1 '18 at 7:01
  • 1
    +1: Awesome. Useful answer. – copper.hat Jun 19 '18 at 17:25
  • On OS X when using the build-in date you will recieve an error. Use brew install coreutils to install GNU's date function, gdate. – CousinCocaine Jun 20 '18 at 10:47
  • doesn't work: $ docker run -it bash bash bash-5.0# date -d "+10 days" date: invalid date '+10 days' bash-5.0# – Christian Bongiorno Feb 13 at 2:49
35

You can use "+x days" as format string:

$ date -d "+10 days"
19
$ date -v -1d

In order to get 1 day back date using date command:

$ date -v -1d

It will give (current date -1) means 1 day before .

$ date -v +1d

This will give (current date +1) means 1 day after.

Similarly below written code can be used in place of "d" to find out year,month etc.

y-Year
m-Month 
w-Week 
d-Day 
H-Hour 
M-Minute  
S-Second
  • 8
    This works for me on my mac, but not on our Gentoo servers. – Mike Dotterer May 6 '15 at 17:19
  • Much better than the accepted answer, this allows for arbitrary date formats too in a single line. – pferrel Nov 9 '15 at 16:48
  • 9
    this does not work on Ubuntu 15.10 or RedHat 6.6. I assume it does not work on RH5.3 either. Reason: '-v' and '-1d' are no valid switches for 'date' – syss Mar 2 '16 at 9:22
  • 2
    This does appear to be a Unix/BSD syntax for date, which GNU's date (gdate on many BSDs) doesn't support. GNU's date uses the syntax in the accepted answer. Always remember: GNU's Not Unix! – Bacon Bits May 18 '16 at 18:08
  • 1
    @BaconBits - but if you're rms you can claim that because a lot of GNU util code has been ported to Linux that therefore "Linux is GNU". Except, of course, for the fact that GNU Hurd (FSF's Once-And-Future-OS) is just barely a thing. I suppose what pisses rms off is that while he spent his time pontificating, Linus spent his time writing code - which turns out to be proof positive that you're much more likely to create working software if you write code than if you pontificate. :-) – Bob Jarvis Jul 6 '16 at 18:41