date time diff (bash)

itry to make date time diff in seconds
looked on the web for different soltuin but nothing works
for example this are my dates
data1=`date +"%d-%m-%YT%H:%M:%S"`
and second date i get from query
values for example :
03-07-2019T17:25:58
03-07-2019T13:00:00

how do i get time diff in seconds?
 


captain-sensible

Active Member
don't know about a direct lazy way but for instance if you do this:
bash-4.3$ t=$(date +%H)
bash-4.3$ echo $t
18
you can get hour
or
bash-4.3$ seconds=$(date +%S)
bash-4.3$ echo $second
13

you can get seconds.
so it wouldn't be to hard to set a date time in elements of %H %M %S at the time a variables or files were created
then a bit of calculation to turn it all into seconds, total it up & compare ?
 

captain-sensible

Active Member
ok i've got a half lazy for you using epoch (time since 1970)
try this:



t1=$(date +%s)
echo $t1

sleep 2m


t2=$(date +%s)
echo $t2
t3=$((t2-t1))

echo $t3
 

Members online


Top