首页 > 解决方案 > NodeJS:为什么 new Date().toLocaleString() 忽略系统时区?

问题描述

我的 /etc/timezone 设置为我的本地时区,这是“timedatectl status”的输出:

      Local time: Wed 2018-09-26 12:01:32 MDT
  Universal time: Wed 2018-09-26 18:01:32 UTC
        RTC time: Wed 2018-09-26 18:01:32
       Time zone: Etc/UTC (MDT, -0600)
 Network time on: yes
NTP synchronized: yes
 RTC in local TZ: no

“日期”命令打印 Wed Sep 26:12:01:32 MDT 2018。

在 Node 中,“new Date().toLocaleString()”会忽略时区并吐出“2018-09-26 18:01:32”。

有没有办法在 Node 中获取本地时间?到目前为止,我看到的所有答案都声称 Node 无法确定运行它的机器的时区,除非您明确设置“TZ”环境变量,这很荒谬。

标签: javascriptnode.js

解决方案


推荐阅读