#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Posts
    77
    Rep Power
    2

    Generating random dates then add x days to that date


    Hi folks,

    I have made a bash script to generate random dates using /bin/date
    The dates are output to the console.

    Now I want to take those dates and add x number of days to them ie. 5 days.

    So an example output would be:

    21-05-2012
    26-05-2012

    My script:

    Code:
    #!/bin/sh
    i=1
    while [ $1 -lt 10 ]
    do
    d=$(date -d "$((RANDOM%1+2012))-$((RANDOM%12+1))-$((RANDOM%28+1))" '+%d-%m-%Y')
    echo "$d"
    i=$(( $i + 1 ))
    done
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    spaceBAR Central
    Posts
    225
    Rep Power
    41
    You can use one of the options of the attached script to perform lots of date functions(date arithmetic, conversions, etc).
    hth
    Attached Files
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    24
    Rep Power
    0
    Thanks for the information it is a very good post now I know about this X-post.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Posts
    77
    Rep Power
    2
    Originally Posted by spacebar208
    You can use one of the options of the attached script to perform lots of date functions(date arithmetic, conversions, etc).
    hth
    It was late and I had to look at the code with fresh eyes. Next morning I took the existing code and manipulated it in a very simple manor.

    Since $d generates a random date in 2012 using /bin/date and random.

    and since date takes the parameter 'days' or 'days ago'. All I needed to do was pass $d to date with days set to 10. This would generate a random date and a date that was 10 days in the future from that previous date.

IMN logo majestic logo threadwatch logo seochat tools logo