Yuuki Chan Yuuki Chan - 4 months ago 21
Bash Question

Bash convert two Epoch times to remaining time

I'm trying to convert two Epoch (Unix timestamp) dates to human readable date (time left)

AT=$(echo $RES | jq '.results[0].unixtime' | tr -d '"') # Returns unix time in JST.
NOW=$(TZ=":Asia/Tokyo" date +%s) # Returns current time in JST
DIFF=$(echo $AT-$NOW | bc)


As an example;

AT=1470038400
NOW=1470032871
DIFF=5529


How do I get the remaining time between the two in the following format: DAYd HOURh MINm SECs?

Answer

You can try the following

printf "%dd %dh %dm %ds\n" $(( DIFF / (3600 * 24) ))  $(( (DIFF / 3600 ) % 24)) $(( (DIFF / 60) % 60)) $((DIFF % 60))
Comments