difftime - Linux


Overview

difftime calculates the difference between two POSIX timestamps. These timestamps can be obtained using commands like date +%s, where %s is the POSIX time format.

Syntax

difftime(end_time, start_time)
  • end_time: The later POSIX timestamp.
  • start_time: The earlier POSIX timestamp.

Options/Flags

None.

Examples

  • Calculate the time difference in seconds between two dates:
echo $(date +%s)
1667526663
echo $(date +%s)
1667526703
difftime 1667526703 1667526663
40
  • Convert time difference from seconds to days:
secs=40
days=$(echo "$secs / 60 / 60 / 24" | bc)
echo $days
0

Common Issues

None.

Integration

  • date: Commands like date +%s can be used together with difftime to calculate time differences from the current time or specific dates.
  • bc: This command can be used to perform calculations on the time difference obtained from difftime.

Related Commands

  • date
  • expr