Shell – Regarding getting Date – n days Date

dateshell

Suppose I have a particular date stored in a variable date_m. I want ((date_m)-25) date.

For example: I have 15/09/2014 stored in my variable , then I want 21/08/2014 returned if I subtract 25 from the date stored in variable.

Best Answer

With the GNU implementation of date, to display yesterday's date, enter:

$ date --date="1 days ago"

OR

$ date --date="-1 day"

For your question:

$ date --date="25 days ago"

OR

$ date --date="-25 day"

For using it with variables, you can use $():

pastDate=$(date --date="-25 day")
echo "$pastDate"

For general case n days and for a specific date:

#!/bin/bash
date1="Tue Sep 2 07:53:47 EEST 2014"
echo "Before? "
read n
date --date="$date1 -$n day"

Source

Related Question