How to subtract a year from a date stored in a variable in shell script?
Played around with it. This seems to work:
as_of_dt='2016-01-01'
as_of_dt_prev_year=$(date --date="${as_of_dt} -1 year" +'%Y-%m-%d')
echo $as_of_dt_prev_year
Note the double quotes that are needed for variable substitution to work.
How to subtract or add date from a variable?
With GNU date
it can be done quite easily with its -d
switch.
x=20170402
date -d "$x -1 days" "+%Y%m%d"
20170401
and for 2 days
date -d "$x - 2 days" "+%Y%m%d"
20170331
subtract days from a date in bash
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)
Date conversion and subtraction in shell script
A quick solution :
datetime=20180123
date -d "$datetime - $date_diff days" +%Y-%m-%d
# -----------------------------^^^^^^
returns
2018-01-21
Note that I had to eliminate the time portion of your time-stamp and used -2 days
for the subtraction.
If you need the time portion, I would recommend saving that to a separate variable then appending that value back onto your output. But as you've changed your output format, I guess the time isn't that important?
IHTH
How to add or subtract from date with format DDMMYYYY in bash?
From man date
The --date=STRING is a mostly free format human readable date string such as "Sun, 29 Feb 2004 16:21:42 -0800" or "2004-02-29 16:21:42" or even "next Thursday". [...] The date string format is more complex than is easily documented here but is fully described in the info documentation.
tl;dr: Seems like you cannot specify the format of the input. Use one of the known formats, for instance
date -d '2019-12-13 -5 day' +%d%m%Y
To automatically convert a date from DDMMYYYY
format to YYYY-MM-DD
you can use sed
...
date -d "$(sed -E 's/(..)(..)(.*)/\3-\2-\1/' <<< 13122019) -5 day" +%d%m%Y
... or bash ...
d=13122019
date -d "${d:4}-${d:2:2}-${d:0:2} -5 day" +%d%m%Y
Today's date, minus X days in shell script
For GNU date
:
date_222days_before_TodayYear=$(date --date="222 days ago" +"%Y")
date_222days_before_TodayMonth=$(date --date="222 days ago" +"%m")
date_222days_before_TodayDay=$(date --date="222 days ago" +"%d")
For BSD date
::
If you are using OS X or FreeBSD, use the following instead because BSD date is different from GNU date:
date_222days_before_TodayYear=$(date -j -v-222d +"%Y")
date_222days_before_TodayMonth=$(date -j -v-222d +"%m")
date_222days_before_TodayDay=$(date -j -v-222d +"%d")
Source: BSD date manual page
Note:
In bash
and many other languages, you cannot start a variable name with a numerical character, so I prefixed them with date_
for you.
Second Update: New requirement - Using 222 Working days instead of 222 Regular days:
(Assumption: Not considering statutory holidays, because that just gets far beyond the scope of what I can help you with in a shell script:)
Consider 222 working days:
- 5 working days per week, that is
floor(222/5) == 44 weeks
44 weeks * 7 days per week == 308 days
- Extra days leftover:
222 % 5 == 2
- Therefore
222 working days == 310 regular days
But, there is a catch! If the number of regular days is 308
or some multiple of 7
, then we would have been fine, because any multiple of 7-days ago from a working day is still a working day. So we need to consider whether today is a Monday or a Tuesday:
- If today is a Monday, we'd get Saturday where we wanted Thursday
- If today is a Tuesday, we'd get Sunday where we wanted Friday
So you see we need an additional offset of 2 more days if today is either Monday or Tuesday; so let's find that out first before we proceed:
#!/bin/bash
# Use 310 days as offset instead of 222
offset=310
# Find locale's abbreviated weekday name (e.g., Sun)
today=$(date -j +"%a")
# Check for Mon/Tue
if [[ "$today" == "Mon" ]] || [[ "$today" == "Tue" ]]; then
offset=$((offset+2))
fi
date_222_working_days_before_TodayYear=$(date -j -v-${offset}d +"%Y")
date_222_working_days_before_TodayMonth=$(date -j -v-${offset}d +"%m")
date_222_working_days_before_TodayDay=$(date -j -v-${offset}d +"%d")
And that should do it =)
Related Topics
How to Read Config Files with Section in Bash Shell
Process Stuck in Exit, Shows as Zombie But Cannot Be Reaped
Linux, Gnu Gcc, Ld, Version Scripts and the Elf Binary Format -- How Does It Work
Installing Multiple Versions of R
How Do Programs Communicate with Each Other
What Length Can a Network Interface Name Have
Limit Useable Host Resources in Docker Compose Without Swarm
How to Avoid "No Such File or Directory" Error for 'Make Clean' Makefile Target
How to Make the Watch Command Interpret Vt100 Sequences
Possibly Undefined MACro: Ac_Prog_Libtool
How to Cd into a Directory Using Perl
What Does Two Dots Before a Slash Mean? (../)