To add days to a date and get the new date on Linux, you can use the date
command with the -d
option and the +%Y%m%d
format string.
Here is an example of how to add 3 days to the current date and display the new date in the YYYYMMDD
format:
new_date=$(date -d "+3 days" +%Y%m%d) echo $new_date
The date -d "+3 days"
command calculates the date 3 days from the current date, and the +%Y%m%d
format string specifies the output format as YYYYMMDD
. The result is stored in the new_date
variable, which can be echoed to display the new date.
To add a different number of days, you can specify the number of days you want to add after the +
sign. For example, to add 5 days, you can use +5 days
. To subtract days, you can use a negative number after the +
sign. For example, to subtract 2 days, you can use -2 days
.
You can also specify a specific date as the starting point by using the -d
option followed by the date in the YYYY-MM-DD
format. For example, to add 3 days to the date 2022-01-01
, you can use the following command:
new_date=$(date -d "2022-01-01 +3 days" +%Y%m%d) echo $new_date
This will output the new date in the YYYYMMDD
format, in this case 20220104
.
Note: The
date
command has many options and formatting strings available for manipulating and formatting dates. Consult the documentation and online resources for more information on the various options and features available with thedate
command.