在 Linux 上 Node.js 日志时间不对,通常不是 Node 本身的问题,而是时区 / 时间来源 / 日志库配置导致的。下面按常见原因逐一说明:
Node.js 默认使用 系统本地时区。
查看系统时间:
date
查看时区:
timedatectl
如果不是你期望的时区(如 Asia/Shanghai),可以修改:
sudo timedatectl set-timezone Asia/Shanghai
✅ 改完一般 Node 日志时间就正常了(无需重启 Node)。
很多人以为 Node 日志时间是“错的”,其实是 UTC 时间。
例如:
console.log(new Date());
输出:
2026-01-15T07:00:00.000Z
这是 UTC 时间(+0),不是中国时间(+8)。
console.log(new Date().toLocaleString());
const winston = require('winston');
const logger = winston.createLogger({
format: winston.format.combine(
winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
winston.format.printf(info => `${info.timestamp} ${info.level}: ${info.message}`)
),
transports: [new winston.transports.Console()]
});
如果你在 Docker / K8s 中运行 Node:
date
发现时间不对,那是因为:
docker run -v /etc/localtime:/etc/localtime:ro your-node-app
ENV TZ=Asia/Shanghai
系统时间本身就是错的,Node 只是“如实记录”。
检查:
timedatectl status
同步时间:
sudo ntpdate pool.ntp.org
# 或
sudo systemctl restart systemd-timesyncd
有些日志库默认就是 UTC:
pm2 env 0 TZ=Asia/Shanghai
pm2 restart all
pino({ timestamp: true })
需手动格式化时间。
你可以按顺序检查:
date 系统时间对不对?timedatectl 时区对不对?Z 结尾(UTC)?Node.js 日志时间不对 ≠ Node 有 bug,99% 是时区或 UTC 显示问题。
如果你愿意,可以把:
发出来,我可以帮你精准定位。