Saturday, October 29, 2022

[SOLVED] Subtract days from a date in Bash

Issue

I want to subtract "number of days" from a date in Bash. I am trying something like this ..

echo $dataset_date #output is 2013-08-07

echo $date_diff #output is 2   

p_dataset_date=`$dataset_date --date="-$date_diff days" +%Y-%m-%d` # Getting Error

Solution

You are specifying the date incorrectly. Instead, say:

date --date="${dataset_date} -${date_diff} day" +%Y-%m-%d

If you need to store it in a variable, use $(...):

p_dataset_date=$(date --date="${dataset_date} -${date_diff} day" +%Y-%m-%d)


Answered By - devnull
Answer Checked By - Gilberto Lyons (WPSolving Admin)