Bash

Days Between Two Dates

admin by @admin ADMIN
3d ago
May 31, 2026
Public
0 0 up · 0 down Sign in to vote
Convert both dates to epoch seconds, subtract, divide. Works for hours/minutes too with the obvious divisor.
Bash
Raw
days_between() {
    local d1 d2
    d1=$(date -d "$1" +%s)
    d2=$(date -d "$2" +%s)
    echo $(( (d2 - d1) / 86400 ))
}

days_between "2025-01-01" "2025-03-15"      # 73
days_between "2025-03-15" "2025-01-01"      # -73   (direction matters)

# Hours between (rounded down)
hours_between() {
    echo $(( ($(date -d "$2" +%s) - $(date -d "$1" +%s)) / 3600 ))
}

hours_between "2025-03-15 09:00" "2025-03-15 17:30"    # 8
Tags

Save your own code snippets

Create a free account and build your private vault. Share publicly whenever you want.