Reputation: 87
I want to calculate the difference in time by passing in a string HH:MM
value to set the time and calculating the difference between this and the current system time.
I'm aware of date --set="2013-07-10"+%T
(or something like this). But I cannot figure out how to set the time part to be the string HH:MM
value that I pass in and leave the data as the current date. Then, to calculate I was trying to use the below which changes it into a timestamp value; which I found from another user:
mydate=??
date1=$($mydate +"%s") #my set date/time here
date2=$(date +"%s")
diff=$(($date2-$date1))
echo "$(($diff / 60)) minutes and $(($diff % 60))"
Upvotes: 0
Views: 3761
Reputation: 274632
You can use date -d hh:mm
to create a new date with a specified time.
Example:
$ date -d 11:15
Wed Jul 10 11:15:00 BST 2013
Using this, you can write the following script:
time=11:15
now=$(date +%s)
other=$(date -d $time +%s)
diff=$((other-now))
echo "$((diff / 60)) minutes and $((diff % 60)) seconds"
Upvotes: 2