在 Ubuntu(Linux)里,“时间戳”通常有两种常见形式,对比大小的方法也不一样:
例如:1717489200
Unix 时间戳本质就是整数,越大表示时间越晚。
t1=1717489200
t2=1717490000
if [ "$t1" -lt "$t2" ]; then
echo "t1 早于 t2"
fi
✅ 适合 秒级或毫秒级整数时间戳
如果是毫秒:
t1=1717489200000
t2=1717490000000
if [ $((t1/1000)) -lt $((t2/1000)) ]; then
echo "t1 更早"
fi
2024-06-04 12:00:00)t1=$(date -d "2024-06-04 12:00:00" +%s)
t2=$(date -d "2024-06-05 12:00:00" +%s)
if [ "$t1" -lt "$t2" ]; then
echo "t1 小于 t2"
fi
date 比较if [[ $(date -d "2024-06-04" +%s) < $(date -d "2024-06-05" +%s) ]]; then
echo "前者更早"
fi
now=$(date +%s)
target=1717489200
if [ "$now" -gt "$target" ]; then
echo "当前时间已过 target"
fi
"2024-06-04" 可以,"06/04/2024" 不行)%s = 秒,%N = 纳秒如果你有具体的时间格式或脚本场景(比如 shell / python / 日志对比),我可以直接给你对应代码。