Search code examples
bashdateformattingdate-arithmetic

Date with specific format like YY-MM-DD


I want to add days in existing fixed date variable. example I have a variable

2017-12-01 

and I want to add 30 days in it.

I used following command

date -d "2017-12-01+30 days"

but its output is

Sun Dec 31 00:00:00 PKT 2017

and I want to get output like this

2017-12-31 

Solution

  • With GNU date:

    date -d "2017-12-01 +30 days" '+%F'
    

    Output:

    2017-12-31
    

    See: man date