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

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 $(( .. ))

Related

bash is eating spaces from date format in linux

date format shows correct when i execute just date but when I store in a variable, loosing a space in date if it has single digit(need that extra space to grep /var/log/messages). please suggest to get the exact format as it is. thanks!
$date -d '-1 day' '+%b %e'
Aug 1
$echo $(date -d '-1 day' '+%b %e')
Aug 1
$var=`date -d '-1 day' '+%b %e'`
$echo $var
Aug 1
Use double-quotes like this:
$ echo $(date -d '+1 day' '+%b %e')
Aug 2
$ echo "$(date -d '+1 day' '+%b %e')"
Aug 2
Or:
$ var="$(date -d '+1 day' '+%b %e')"
$ echo $var
Aug 2
$ echo "$var"
Aug 2
Without double-quotes, the shell, among other things, applies word splitting to the output and that collapses multiple spaces to one blank.

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

Get the next time occurance with linux date

Linux date utility can understand a lot of strings including for instance:
$ date -d '8:30'
Fri Jan 2 08:30:00 CET 2015
I'm looking for a way to get the next 8:30, thus:
in case it is Fri Jan 2 before 8:30, the result above should be returned;
otherwise it should print Sat Jan 3 08:30:00 CET 2015.
As one can see next 8:30 doesn't result in the correct answer:
$ date -d 'next 8:30'
date: invalid date ‘next 8:30’
Is there a single expression to calculate this?
Handling it in the shell oneself is of course an option, but makes things more complicates because of daylight save time regulation etc.
In case the clock is adapted to daylight save time, next 8:30 should be parsed to 8:30 according to the settings of the next day.
Testcase:
Given it is Fri Jan 2 12:01:01 CET 2015, the result should be:
$ date -d 'next 8:30'
Sat Jan 3 08:30:00 CET 2015
$ date -d 'next 15:30'
Fri Jan 2 15:30:00 CET 2015
Just use something like:
if [[ $(date -d '8:30 today' +%s) -lt $(date +%s) ]] ; then
next830="$(date -d '8:30 tomorrow')"
else
next830="$(date -d '8:30 today')"
fi
The %s format string gives you seconds since the epoch so the if statement is basically:
if 8:30-today is before now:
use 8:30-tomorrow
else
use 8:30-today
I researched and it does not seem to be possible to do so.
What you can probably do is to compare the hour and minute with 830 and print accordingly:
[ $(date '+%H%M') -le 830 ] && date -d '8:30' || date -d '8:30 + 1 day'
In case you want to work with this easily, create a function to do these calculations.
Test
$ [ $(date '+%H%M') -le 830 ] && date '8:30' || date -d '8:30 + 1 day'
Sat Jan 3 08:30:00 CET 2015

How to make date command work in relation to custom specified date?

In Linux there is pretty awesome command date which can be used is ways like this:
# Get some cool date in relation to systems date:
date -d "last Sunday -7 days"
Sun Sep 15 00:00:00 PDT 2013
# Set systems date:
date --set="2013-03-04"
Mon Mar 4 00:00:00 PST 2013
Basically I want to be able to run this command like this:
date --date="last Sunday -7 days" +%Y-%m-%d
2013-09-15
But not in relation to today's system date but in relation to some date generated by another computation in the form of string (e.g. "2013-09-01") or something else.
Please help me to figure out how to do that.
Using a function:
function get_last_day {
local date=$1 day=$2 format=$3 a b i
for (( i = 0; i <= 6; ++i )); do
read -r a b < <(exec date -d "$date - $i days" "+%a $format")
if [[ $a == "$day" ]]; then
echo "$b"
return
fi
done
}
get_last_day '2013-09-18' Sun '%Y-%m-%d'
Output:
2013-09-15

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