温馨提示×

centos如何输出时间戳

小樊
35
2026-10-20 07:45:29
栏目: 智能运维

在 CentOS(本质上是 Linux)里“输出时间戳”常见有几种场景,下面按最常用的方式给你说明:


一、输出当前 Unix 时间戳(秒级)

date +%s

示例输出:

1719301234

二、输出毫秒级时间戳

date +%s%3N

或

date +%s.%N

示例:

1719301234567

三、输出可读时间 + 时间戳

date "+%Y-%m-%d %H:%M:%S %s"

示例:

2024-06-25 14:20:34 1719301234

四、Shell 变量中使用时间戳

TS=$(date +%s)
echo $TS

五、日志中常用(带时间戳)

echo "$(date '+%F %T') this is a log"

输出:

2024-06-25 14:20:34 this is a log

六、Python 方式(如果装了 Python)

python3 -c "import time; print(int(time.time()))"

如果你是指:

  • 脚本里用
  • 写日志
  • 计算时间差
  • 定时任务(crontab)

可以告诉我具体场景,我可以给你更合适的写法。

0 踩