How to write a shell script where to add days month and year to current date

13,603

Solution 1

Tip: fire up a terminal(in my case bash terminal)

the help is a good starting point

date --help

or man page

man date

A lot of information and examples.

Date manipulation in bash (copy paste the example run in your terminal):

add 10 days to the current date:

date -d "10 day" +"%Y %m %d"

or remove 10 days to the current date

date -d "-10 day" +"%Y %m %d"

add 2 months to the current date:

date -d "2 month" +"%Y %m %d"

remove 2 months from the current date:

date -d "-2 month" +"%Y %m %d"

add 1 year to the current date

date -d "1 year" +"%Y %m %d"

remove 1 year to the current date

date -d "-1 year" +"%Y %m %d"

mixing add 1 year month and day

date -d "1 year 1 month 1 day" +"%Y %m %d"

in a script (in my case bash)

foobaa=`date -d "1 year 1 month 1 day" +"%Y %m %d"`
echo $foobaa

I hope it helps a little..

Solution 2

The equivalent on Mac OSX is date -v "+60M" to add 60 minutes.

Share:
13,603
Sara
Author by

Sara

Updated on June 05, 2022

Comments

  • Sara
    Sara almost 2 years

    I am new to shell script.

    I want to write a script where I add days month and years to current day. for example it take current date and every time addd 3 to day 4 to months and 2 to years. all the three things given as arguments.

    I would really appreciate it.

    Br