The Bash script below does basically what jan61's post above does. Input dates in the same format to get total days, hours, minutes between two dates.
Code:
#!/bin/bash
read -p "Date1: " d1
read -p "Date2: " d2
d3=$(date --date "$d1" +%s)
d4=$(date --date "$d2" +%s)
d5=$(($d4 - $d3))
printf "Days: "
echo "scale=0; (${d5} / 86400) " | bc
printf "Hours: "
echo "scale=0; ((${d5} / 86400) * 24) " | bc
printf "Minutes: "
echo "scale=0; ((${d5} /86400) * (24) * 60)" | bc
The bash script below here takes same input format to seconds and produces the difference between two dates not just total days, hours, minutes. Example enter date: 2009-03-03 02:30:15, and second date:
2009-03-05 10:45:30, get output:
Days: 2
Hours: 8
Minutes: 15
Seconds: 15
Code:
#!/bin/bash
read -p "Date1: " d1
read -p "Date2: " d2
d3=$(date --date "$d1" +%s)
d4=$(date --date "$d2" +%s)
d5=$(($d4 - $d3))
printf "Days: "
echo "scale=0; (${d5} / 86400) " | bc
printf "Hours: "
echo "scale=0; ((${d5} % 86400) / 3600) " | bc
printf "Minutes: "
echo "scale=0; ((${d5} % 86400) % 3600) / 60 " | bc
printf "Seconds: "
echo "scale=0; ((${d5} % 86400) % 3600) % 60 % 60" | bc
First actual Bash scripts I've done so I imagine there are easier ways to do it. They work! Been playing around trying to get what I wanted to work here for so long I forgot why I was doing it.