How to find date n days in the future from past date? - bash

I want to find a date that is 57 –working– days after an arbitrary date using date or bash. For instance,
today is august 21st,
reference date is july 15th,
what days will be 57 working days after july 15th?

This should work to just get all days
date -d '7/15/14 +57 days'
To get number of work days (M..F) you can do something lazy like this
#!/bin/bash
days=0
for ((i=1;i>0;i++)) do
future=$(date -d "7/15/14 +$i days" '+%w')
((future!=0 && future!=6)) && ((days++)) # ignore sunday (0) and saturday (6)
((days==57)) && date -d "7/15/14 +$i days" && break
done
e.g.
> ./abovescript
> Thu Oct 2 00:00:00 CDT 2014

Weird solution:
day=21
mon=8
year=2014
days=4
curl -s "http://www.timeanddate.com/scripts/dateserver.php?mode=addweekdays&d1=$day&m1=$mon&y1=$year&type=add&ad=$days&atyp=0&ach=3" | sed -n 's|.*<h2>Result: \(.*\)</h2>.*|\1|p'
prints
Wednesday, August 27, 2014
the date after 4 working days from 2014.08.21
for the
day=15
mon=7
year=2014
days=57
prints
Friday, October 3, 2014

Related

Subtract hours from date object in bash

I need to find a time X hours before the previous midnight, thus I would like to subtract X hours from a date object.
Example
# Finding the previous midnight
date -d "yesterday 23:59:59"
Mon Jul 11 00:00:00 CEST 2022
What I want
I would like to find the date X hours before this midnight
x=4
Mon Jul 10 20:00:00 CEST 2022
You can use this date command:
date -d "today 0 -4 hours"
Here:
today 0: gets midnight date-time for today's date
-4 hours: subtracts 4 hours from midnight time
It seems one can just write the following:
date --date 'yesterday 23:59:59 CEST -4 hours'

Get saturday date for the given input date using bash script

By using the below command, it will return the last saturday date.
date +"%b-%d-%Y" -d "last saturday"
Sep-01-2018
I want to pass input date as parameter, which should return the last saturday's date in bash script.
Aug-08-2018 -----> Aug-04-2018
Jun-04-2018 -----> Jun-02-2018
Get a negative number that will be the number of days to subtract. We use 13, because Saturday is 6, and 6 + 7 = 13. This will get us the Saturday one or two weeks ahead. Then we modulo 7, to ensure it is NEXT Saturday, then subtract 7 to make it LAST Saturday. Then we put that diff into the date string:
$ date_str="Aug-08-2018"
$ diff=$(( (13 - $(date +"%u" -d ${date_str})) % 7 - 7))
$ date -d "${date_str} ${diff} days"
Sat Aug 4 00:00:00 EDT 2018

Determining week number on a 4 week cycle in bash script

what I want is a cycle of 4 weeks in a bash script
My question is: How do I know this week's number in the cycle.
week x monday : echo one
week x+1 monday : echo two
week x+2 monday : echo three
week x+3 monday : echo four
and again
week x+4 monday : echo one
and so on
what I have is the epoch
(UTC), Thursday, 1 January 1970
consequently
(UTC), monday, 5 January 1970 (I can set this to echo 1)
Any suggestions? Converting dates is no problem. Just a general idea is ok.
I think you are expecting do something like this, with GNU date,
start_date=$(date -d "1970-01-05" '+%s') # Corresponding to 1
end_date=$(date -d "2017-01-02" '+%s') # Current week
Number of weeks between the dates
numberOfWeeks=$(( ( end_date - start_date )/(60*60*24*7) ))
printf "%s\n" "$numberOfWeeks"
2452
Now to determine which week this corresponds to, do
printf "The current week %s belongs to week %d" "$(date)" "$(((numberOfWeeks%4) + 1))"
The current week Mon, Jan 02, 2017 4:47:09 PM belongs to week 1
For further weeks down the line, say. 4th Monday of March 2017, using the above computation, i.e. with
end_date=$(date -d "2017-03-27" '+%s')
printf "The week %s belongs to week %d" "$(date -d "2017-03-27")" "$(((numberOfWeeks%4) + 1))"
The week Mon, Mar 27, 2017 12:00:00 AM belongs to week 1
Another example for the 3rd Monday or March 2017,
end_date=$(date -d "2017-03-20" '+%s')
printf "The week %s belongs to week %d" "$(date -d "2017-03-20")" "$(((numberOfWeeks%4) + 1))"
The week Mon, Mar 20, 2017 12:00:00 AM belongs to week 4
You can format the date output for showing the week number:
function printweek {
weeknr=$(date '+%V' -d "+$1 weeks")
echo "$((weeknr%4))"
}
# Test
for week in 0 1 2 3 4 5 6 30 31 32 33; do
echo "Week offset from today ${week} => $(printweek ${week})"
done
This will work when you start over counting each year (first week 1 again). When you want to continue counting on 1 Januari, the script will be more difficult. You can look at the solution of #Inian.
Another option might be looking at the output of the last run, and add one %4 to the weeknumber of the last run.

How to subtract 5 minute from date

I have this data:
`date +%Y-%m-%d`" 00:00:00"
that return 2015-10-08 00:00:00
I would like cancel 5 minute:
2015-10-07 23:55:00
Many thanks
You need to subtract 5 minutes from a known point in time:
$ date -d "00:00:00 today"
Thu Oct 8 00:00:00 EDT 2015
$ date -d "00:00:00 today -5 minutes"
Wed Oct 7 23:55:00 EDT 2015
You just need to add your format string.
There's more than one way to subtract a value from the current time, although this should match the format shown in your question:
date -d "-5 min" "+%Y-%m-%d %H:%M:%S"
Result:
2015-10-08 15:26:13

How do I get the month one calendar month ago in bash? [duplicate]

This question already has answers here:
Using `date` command to get previous, current and next month
(5 answers)
Closed 8 years ago.
I have been using the command:
date --date='1 months ago' +%b
To get the month name of the month it was a month ago, but have realised today as it is the 31st that this command actually gives me the month name it was 4 weeks ago.
Is there any way to get the calendar month that it was 1 month ago, or indeed n months ago as I can see that the discrepancy will be greater as the number of months is longer.
Date calculations that depend on the number of days in the month are tricky. A hybrid approach, using month numbers and a lookup table, will probably work best.
months=("" Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)
echo ${months[$(date +%m) - 1 ]}
[[ $(date +%d) == "31" ]] && date -d'-31 day' +%b || date -d'-1 month' +%b
test with today:
kent$ date
Thu Jul 31 17:34:27 CEST 2014
kent$ [[ $(date +%d) == "31" ]] && date -d'-31 day' +%b || date -d'-1 month' +%b
Jun
try this one line
#if the month before 30 days is the same of the actual month ,then return the month before 31 days
[[ `date --date='30 day ago' +%b` == `date +%b` ]] && echo `date --date='31 day ago' +%b` || echo `date --date='30 day ago' +%b`

Resources