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)
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
How subtract days from date in bash shell on Solaris 11?
If you don't have GNU date or GNU awk, consider perl
:
subtractDays() {
local date numDays
date=$1
numDays=$2
date=$date days=$numDays perl -e '
use Env qw(date days);
use Time::Piece;
use Time::Seconds;
my $start_time = Time::Piece->strptime($date, "%Y-%m-%d");
my $end_time = $start_time - (ONE_DAY * $days);
print $end_time->ymd . "\n";'
}
...thereafter:
subtractDays 2000-12-31 1
...emits...
2000-12-30
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 =)
Get the date (a day before current time) in Bash
Sorry not mentioning I on Solaris system.
As such, the -date switch is not available on Solaris bash.
I find out I can get the previous date with little trick on timezone.
DATE=`TZ=MYT+16 date +%Y-%m-%d_%r`
echo $DATE
Bash Shell Current Date Minus Number of Days
Try doing this :
#!/bin/sh
#
# BACKUP DB TO S3
#
# VARIABLES
TYPE="DATABASE"
DAYS="30"
# GET CURRENT DATETIME
CURRENTDATE="$(date +%Y%m%d%H%M%S)"
# GENERATE PAST DATE FROM DAYS CONSTANT
OLDERDATE="$(date "+%Y%m%d%H%M%S" -d "$DAYS days ago")"
# CALL PYTHON SCRIPT WITH OLDERDATE ARGUMENT
python script.py "$OLDERDATE"
See info coreutils 'date invocation' | less +/28.7\ Relative\ items\ in\ date\ strings
How to subtract today's date with a file's modification date in unix?
Use %s seconds since 1970-01-01 00:00:00 UTC as in
echo $(expr $(date +%s) - $(date -r sample.txt +%s)) #!/bin/sh
echo $(($(date +%s) - $(date -r sample.txt +%s))) #/bin/bash
How to subtract two different date formats to get days in bash?
Try this:
let diff=(`date +%s -d 11/2/2015` - `date +%s`)/86400
echo $diff days remaining
Related Topics
Xdp Program Ip Link Error: Prog Section Rejected: Operation Not Permitted
Git Sparse-Checkout Ignore Specific File Type
How to Duplicate String in Bash
Rename Part of File Name Based on Exact Match in Contents of Another File
Linux Script to Automate Ftp Operation
If I Have a Process, and I Clone It, Is the Pid the Same
String Comparison Not Working Properly
While Do Loop and Variables in a Bash Script
Python Socket.Error: [Errno 13] Permission Denied
Insert New Line to Bash Prompts
How to Wait for First Command to Finish
Stty Serial Port Settings for Parity Not Persistent
Count Occurrences of Character Per Line/Field on Unix
How to Divide in the Linux Console
Why I Cannot Override Search Path of Dynamic Libraries with Ld_Library_Path