I need to create three variables, each for Year, Month, and Day for Today's date, minus X number of days. For this question I'll choose a random amount of days: 222.
So if:
TodayYear=`date +%Y` TodayMonth=`date +%m` TodayDay=`date +%d`
What I want is 222 days before this.
222days_before_TodayYear=??? 222days_before_TodayMonth=??? 222days_before_TodayDay=???
Edit: Need 222 working days instead 222 regular days.
Set it to the number of days that you want to subtract. Very slight improvement to the command - date --date="${dataset_date} -${date_diff} day" +%Y-%m-%d.
The easiest way is to convert the date to a unix time_t value (i.e. seconds since the beginning of the epoch, or '1-1-1970 00:00:00'), and then substract 30 days * 86400 seconds per day from that number. e.g. the following example uses set -x so that you can see the value of the D variable as it changes.
The "x" has no special meaning and could be replaced with any non-null string.
$() – the command substitution. ${} – the parameter substitution/variable expansion.
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:
floor(222/5) == 44 weeks
44 weeks * 7 days per week == 308 days
222 % 5 == 2
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:
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 =)
date '+%Y' --date='222 days ago'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With