[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: difference in seconds between two formatted dates ...



Hello,

On Sun, Dec 17, 2023 at 10:12:11AM +0000, Albretch Mueller wrote:
> dt00=$(date +%Y%m%d%H%M%S)
> echo "// __ \$dt00: |${dt00}|"
> 
> ... after some long processing for which seconds would be exact
> enough, then I would like to get the seconds elapsed since dt00, like
> this:
> 
> dt02=$(date +%Y%m%d%H%M%S)
> echo "// __ \$dt02: |${dt02}|"
> 
> you get seconds in dt00 and dt02 and then the difference. You can't
> go: $(( dt02 - dt00 )) because bash Arithmetic is 10-based.

Why don't you just get the time as epoch time e.g. "date +%s" and
then you can subtract one from the other¹.

You can also take a copy of the contents of /proc/uptime twice and
subtract one from the other.

Thanks,
Andy

¹ Pedants at this point may feel the need to launch launch into a
sub-thread about how subtracting one epoch time from another doesn't
always produce an accurate duration. I can't stop you, but it won't
be news to me.

-- 
https://bitfolk.com/ -- No-nonsense VPS hosting


Reply to: