这个问题可能与服务器的时区设置有关。通常在服务器中,如果时区设置与前端应用使用的时区不一致,会导致时间显示错误,例如时差 8 小时问题,需要检查数据库的时区格式,服务器的时区,以及后台代码返回的格式数据。
1. 检查和设置服务器的时区
查看当前服务器时区
timedatectl
你会看到类似以下的输出:
Local time: Sun 2024-12-31 14:00:00 CST
Universal time: Sun 2024-12-31 06:00:00 UTC
RTC time: Sun 2024-12-31 06:00:00
Time zone: Asia/Shanghai (CST, +0800)
如果时区不是 Asia/Shanghai,可能是问题的原因。
设置时区为中国时区
sudo timedatectl set-timezone Asia/Shanghai
同步时间
sudo apt update
sudo apt install -y ntpdate
sudo ntpdate time.windows.com
再次确认时区设置是否正确:
timedatectl