Comparing two dates

How to:

Here’s a quick way to compare two dates in Bash:


# Convert dates to seconds since the epoch
sec1=$(date -d "$date1" +%s)
sec2=$(date -d "$date2" +%s)

# Compare the dates
if [ $sec1 -eq $sec2 ]; then
    echo "Dates are the same."
elif [ $sec1 -lt $sec2 ]; then
    echo "Date $date1 is earlier than $date2."
    echo "Date $date1 is later than $date2."

Sample output if $date2 is later:

Date 2023-04-01 is earlier than 2023-04-15.

Deep Dive

Historically, comparing dates in shell scripts wasn’t straightforward due to different date formats and lack of built-in functions. The date command, with %s to convert dates to seconds since the Unix epoch (00:00:00 UTC on 1 January 1970), is a godsend.

Alternatives include using external tools like awk or doing string comparison – risky if formats vary. Implementation-wise, one quirk is dealing with time zones: adding TZ=UTC before date commands ensures UTC comparisons.

Date arithmetic, such as finding the difference between dates, can get complex. Adding or subtracting days requires more date trickery. Corner cases, like leap seconds or daylight saving transitions, can introduce errors.

See Also