Date command go back to 30 days from a particular date - linux

ADate=`date -d"10 days ago" +%s`
BDate=`date -d"$BDate - 30 days" +%s`
It is throwing error as invalid date.

try this:
ADate=`date -d"10 days ago"`
BDate=`date -d"$ADate - 30 days" +%s`
echo $BDate

This works for me:
ADate=$(date -d"10 days ago" +Y%-%m-%d")
BDate=$(date -d"$ADate - 30 days" +%s)
echo $BDate
Output: 1453417200

Related

Trying to add 1 day to a timestamp in bash script but it's only adding 19 hours

I have a bash script where I'm pulling the date from the last line of a text file, adding 1 day to the time and then writing that date back to the file. The idea is to add 24 hours each time. My code look like the following:
start_date=$(date -d "$(tail -n 1 run_dates.txt) +1 day" '+%F %T')
echo "$start_date" >> run_dates.txt
The output file (run_dates.txt) looks like this:
2018-09-18 16:42:57
2018-09-19 11:42:57
2018-09-20 06:42:57
2018-09-21 01:42:57
2018-09-21 20:42:57
For some reason it is only adding 19 hours every time, not a full day. Any idea what this is?
date's free-form date parser seems to get pretty confused with + something at the end of a date-time. All the gory details here: https://www.gnu.org/software/coreutils/manual/html_node/Date-input-formats.html
I get similar results:
$ tail -n 1 run_dates.txt
2018-09-21 20:42:57
$ date -d "$(tail -n 1 run_dates.txt) +1 day" '+%F %T'
2018-09-22 15:42:57
but if you ask for "tomorrow" instead of "+1 day":
$ date -d "$(tail -n 1 run_dates.txt) tomorrow" '+%F %T'
2018-09-22 20:42:57
For adding/subtracting interval to a date command don't use "+". If you omit + it adds, for subtraction you can use "ago" keyword along with the date part. Here are some examples
> date -d "2018-09-21 20:42:57 1 day 2 hours" "+%F %T"
2018-09-22 22:42:57
> date -d "2018-09-21 20:42:57 1 day ago 2 hours ago" "+%F %T"
2018-09-20 18:42:57
> date -d "2018-09-21 20:42:57 1 day 2 hours 12 minutes" "+%F %T"
2018-09-22 22:54:57
> date -d "2018-09-21 20:42:57 1 day 2 hours ago 12 minutes" "+%F %T"
2018-09-22 18:54:57
>

Shell script to print a range of dates with a 6 hour increment

I got a shell script which prints date from one given date to another with six hour difference. I need hour 00,06,12 and 18. It does perfectly that up to some dates and then prints 01,07,13,19. I can not understand what is the reason. Here is the script:
#!/bin/bash
start=$(date --date '1 jan 1998 00:00' +%s)
stop=$(date --date '31 dec 1998 18:00' +%s)
for t in $(seq ${start} 21600 ${stop})
do
idate1=`date --date #${t} +'%Y%m%d%H'`
idate2=`date --date #${t} +'%Y%m%d'`
iyr1=`date --date #${t} +'%Y'`
imon1=`date --date #${t} +'%m'`
iday1=`date --date #${t} +'%d'`
# sleep 2s
echo $idate1
done

I have two dates and need to find the difference in hours

Wed Jan 21 20:44:20 EST 2015
Wed Jan 21 19:04:20 EST 2015
I have two dates about, need to get the difference in minutes. Please help
"c=date -d #$(( $(date -d "$b" +%s) - $(date -d "$a" +%s) )) -u +'%H:%M'" -> This command is giving in HH:MM but i want in MM
Thank you
this gives you the result: 100 minutes:
echo $((($(date -d "$a" +%s) - $(date -d "$b" +%s))/60 ))
Note that, it will always give an int value, if you need the precision less than 1 min, like 100.25 you may want to use bc or awk to do the calculation instead of $(( .. ))

Weekly Cron Job

I want to create Cron job that will run evey 2 weeks on Sunday , I tried this but get Error bad day of week
47 15 * * SUN/2 export DISPLAY=:0 && /usr/lib/jvm/jdk1.6.0_21/jre/bin/java -jar /home/ahmed/Projects/DimensionProject/ProviderJar/FtpDownload.jar LightningSource /home/ahmed/NetBeansProjects/trunk/BookDimensionProject/build/web/linconfig.xml
so any one can help please??
We could use date +%s to obtain the number of seconds since the Epoch, convert that to weeks (604800 seconds = 1 week), and run the cron job only on odd weeks:
47 15 * * SUN test $(expr $(date +%s) / 604800 % 2) -eq 1 && echo "Every other Sunday"

Bash relative date (x days ago)

I have a date string that I am able to parse and format with the date command from a bash script.
But how can I determine how many days ago this date was from my script? I would like to end up with a number.
Use date itself as date value for date.
Example 5 days ago:
date -d "`date`-5days"
You can do some date arithmetics:
DATE=01/02/2010
echo $(( ( $(date +%s) - $(date -d "$DATE" +%s) ) /(24 * 60 * 60 ) ))
Convert your date and now into seconds since the epoch, subtract, divide by the number of seconds in a day:
#!/bin/bash
((a = `date -d "Wed Jan 12 02:33:22 PST 2011" +%s`))
((b = `date +%s`))
echo $(( (b-a) / (60*60*24)))

Resources