【发布时间】:2020-04-15 12:20:32
【问题描述】:
我正在努力使用 Moment 库获取 UNIX 格式的开始和结束日期值。
我尝试了以下方法:
console.log(moment().startOf('day').format()); // works correctly - 2020-04-15T00:00:00+02:00
console.log(moment().startOf('day').format('X')); // not what I need - 1586901600 = 04/14/2020 @ 10:00pm (UTC) (so minus 2h for the timezone)
所以只是为了确保我发送的时间正确:
console.log(moment.tz("2020-04-15T00:00" , "Europe/Warsaw").unix()); // again not what I need - 1586901600 = 04/14/2020 @ 10:00pm (UTC) (so minus 2h for the timezone)
console.log(moment().startOf('day').utcOffset(moment().tz(process.env.TIMEZONE).startOf('day').format('x')).format('X')) // again not what I need - 1586901600 = 04/14/2020 @ 10:00pm (UTC) (even with UTC offset)
我读到你实际上不能偏移或时区 UNIX 时间戳,这在任何地方都是一样的。我很好,但是从 UNIX 时间戳中自动扣除 "Europe/Warsaw" 的“优雅方式”是什么。例如,我在配置文件中设置了时区,我希望 JS 根据时区检测偏移量并正确地从 UNIX 时间戳中扣除。
非常感谢任何帮助。
【问题讨论】:
-
能否告诉我们,您的问题是否已经解决?
标签: javascript momentjs