30

For example, I have date: 4 August 1993 and I want to add 348 days to it, how can I do it in bash?

3 Answers 3

35

Just use the date command with -d option:

$ date -d "1983-08-04 348 days"
Tue Jul 17 00:00:00 BST 1984  

You can change the output format if you want:

$ date -d "1983-08-04 2 days" +%Y-%m-%d
1983-08-06                                           
2
  • 1
    You can use the OP's date format, too: date -d "4 August 1993 348 days" +"%d %B %Y" Sep 23, 2009 at 16:54
  • 2
    According to man date: %F full date; same as %Y-%m-%d
    – jperelli
    May 2, 2012 at 21:58
19

In bash on Mac OS X, you can do this:

date -j -v +348d -f "%Y-%m-%d" "1993-08-04" +%Y-%m-%d

Output: 1994-07-18

3
  • 3
    Been looking for this for a while. I appreciate. I wanted to replace the number "348" with a variable from a bash script. I ended up with NEXT_DATE=$(date -j -v +$(( incrementDays ))d -f "%Y-%m-%d" "1993-08-04" +%Y-%m-%d) for anyone else looking to do this.
    – Ian G
    Jan 15, 2019 at 21:11
  • Nice! There's a whole slew of formatting options you can use with the Bash date command at tutorialkart.com/bash-shell-scripting/…
    – AFK
    Sep 18, 2021 at 3:56
  • You can't believe how many incorrect answers I've stumbled upon until this one. Thanks!
    – Matthew
    Oct 6, 2022 at 18:03
1

Here is a little more complex usage of this:

for i in `seq 1 5`;
do;
  date -d "2014-02-01 $i days" +%Y-%m-%d;
done;

or with pipes:

seq 1 5 | xargs -I {} date -d "2014-02-01 {} days" +%Y-%m-%d

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .