Skip to content
Advertisement

How to get difference between 2 dates (Z format) in milliseconds

Well I’m trying to get the difference between two dates in Seconds.MilliSeconds The dates are in Zulu format

I have tried these two approaches doesn’t work out for me

ms1=$(date -d '2022-04-22T03:47:56.744551446Z' +%N)
ms2=$(date -d '2022-04-22T03:47:57.095419744Z' +%N)
msdiff=$((ms1 - ms2))
echo "$msdiff"
$ dateutils.ddiff  2022-04-22T03:47:56.744551446Z 2022-04-22T03:47:57.095419744Z -f '%N'
0s

Is there any better way to get the difference in Seconds.MilliSeconds In linux for Z format time zones

Advertisement

Answer

Suggesting

ms1=$(date -d '2022-04-22T03:47:56.744551446Z' +%s.%N)
ms2=$(date -d '2022-04-22T03:47:57.095419744Z' +%s.%N)
msdiff=$(awk "BEGIN{print $ms2 - $ms1}")
echo "msdiff=$msdiff"

Output:

msdiff=0.350868
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement