56

I have a Bash script that takes an argument of a date formatted as yyyy-mm-dd.

I convert it to seconds with

startdate="$(date -d"$1" +%s)";

What I need to do is iterate eight times, each time incrementing the epoch date by one day and then displaying it in the format mm-dd-yyyy.

3
  • 2
    Where did you get stuck? Commented Sep 9, 2013 at 20:53
  • On the Mac, you can convert the date into another format (like seconds) add the appropriate constant (like the number of seconds in a day) and then convert the data back all using the date command. I don't know if you can do that on Linux with the date command. You might be able to do this with gawk, and you can definitely do this in Perl or Python. Will those solutions work for you?
    – David W.
    Commented Sep 9, 2013 at 21:20

6 Answers 6

125

Use the date command's ability to add days to existing dates.

The following:

DATE=2013-05-25

for i in {0..8}
do
   NEXT_DATE=$(date +%m-%d-%Y -d "$DATE + $i day")
   echo "$NEXT_DATE"
done

produces:

05-25-2013
05-26-2013
05-27-2013
05-28-2013
05-29-2013
05-30-2013
05-31-2013
06-01-2013
06-02-2013

Note, this works well in your case but other date formats such as yyyymmdd may need to include "UTC" in the date string (e.g., date -ud "20130515 UTC + 1 day").

5
  • 2
    I was able to successfully increment the date in format "yyyymmdd" without using UTC, in Ubuntu machine, with date +%Y%m%d -d "20130515 + 1 day"
    – belindanju
    Commented Mar 3, 2017 at 20:45
  • 7
    Maybe point out that this is not portable across platforms. The -d option is correct for GNU date (i.e. Linux et al.) but not for *BSD date (MacOS etc).
    – tripleee
    Commented Dec 17, 2018 at 12:54
  • 7
    @tripleee: You're right! Here's how to add a day with macOS' date: date -v +1d -jf %F 1999-12-31 +%F which produces 2000-01-01. Commented Apr 25, 2020 at 14:41
  • 1
    to expand @MatthiasBraun, to make your full original answer work for macOS, the NEXT_DATE line should be NEXT_DATE=$(date -v +$((i))d -jf %F $DATE +%F)
    – pjdrew
    Commented Dec 23, 2021 at 17:06
  • 1
    Not portable across all Unix OSes Commented Apr 21, 2023 at 12:43
7
startdate=$(date -d"$1" +%s)
next=86400 # 86400 is one day

for (( i=startdate; i < startdate + 8*next; i+=next )); do
     date -d"@$i" +%d-%m-%Y
done
8
  • 1
    Are you sure adding 1 to each time is correct? It's an epoch date at that point and adding 1 would seem to be seconds and doesn't seem to affect the date at all...I get 8 of the same.
    – JAyenGreen
    Commented Sep 9, 2013 at 21:08
  • 1
    Whoops, didn't notice that OP wanted it to be incremented by one day. Fixed now. Also note that it will start from the input day. Commented Sep 9, 2013 at 21:21
  • It is wrong to assume each day has 86400 seconds. You are totally missing any leap seconds.
    – ceving
    Commented Sep 10, 2013 at 9:06
  • @ceving are you sure? I thought that leap seconds exist only in UTC and date +%s wouldn't ever use leap seconds. Am I wrong? Commented Sep 10, 2013 at 11:37
  • 1
    @Aleks-DanielJakimenko But it seems to me that your calculation works. But not because it is correct, but because GNU date does not handle leap seconds correctly. ;-)
    – ceving
    Commented Sep 10, 2013 at 21:54
2

Just another way to increment or decrement days from today that's a bit more compact:

$ date %y%m%d ## show the current date
$ 20150109
$ ## add a day:
$ echo $(date %y%m%d -d "$(date) + 1 day")
$ 20150110
$ ## Subtract a day:
$ echo $(date %y%m%d -d "$(date) - 1 day")
$ 20150108
$ 
4
  • 1
    To be correct need to put plus sign before the format string. Also there are even more compact ways to do this. ex echo $(date +%y%m%d -d "-1 day")
    – swdev
    Commented Jan 11, 2018 at 18:44
  • The echo is useless, too.
    – tripleee
    Commented Apr 25, 2020 at 15:26
  • triplee, the echo was simply for demonstration purposes. Commented Aug 13, 2020 at 14:58
  • 1
    date: the argument ‘%y%m%d’ lacks a leading '+'; when using an option to specify date(s), any non-option argument must be a format string beginning with '+' Try 'date --help' for more information. Commented Feb 14, 2022 at 2:48
2

Increment date in bash script and create folder structure based on Year, Month and Date to organize the large number of files from a command line output.

for m in {0..100}
do
    folderdt=$(date -d "Aug 1 2014 + $m days" +'%Y/%m/%d')
    procdate=$(date -d "Aug 1 2014 + $m days" +'%Y.%m.%d')
    echo $folderdt
    mkdir -p $folderdt
    #chown <user>:<group> $folderdt -R
    cd $folderdt
    #commandline --process-date $procdate
    cd -
done
2

There is another way similar to this, may not be as fast as adding 86400 seconds to the day, but worth try -

day="2018-07-01"
last_day="2019-09-18"
while [[ $(date +%s -d "$day") -le $(date +%s -d "${last_day}") ]];do 
    echo $i;    
    # here you can use the section you want to use
    day=$(date -d "$day next day" +%Y-%m-%d); 
done
1

date -d is not portable across OSes, and sometimes not reliable at all.

With Perl with core modules (installed by default), this is a robust, portable and reliable way in a shell:

perl -MTime::Piece -MTime::Seconds -sE '
    my $d = Time::Piece->strptime($mydate, "%Y-%m-%d %H:%M:%S");
    ($dow, $month, $dom, $hour, $year) = split / +/, $d +ONE_DAY;
    my $dateold = Time::Piece->strptime(
        "$dow $month $dom $year $hour",
        "%a %b %d %Y %H:%M:%S");
    say $dateold->strftime("%Y-%d-%m %H:%M:%S");
' -- -mydate='2023-06-03 06:21:33'
2023-07-03 06:21:33   

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.