当我在us-east-1托管的AWS Lambda中运行以下命令时

const currentTime = moment().utc().valueOf();
console.log(new Date( currentTime ));//1538003255995
Prints: Wed Sep 26 2018 19:07:35 GMT-0400 (Eastern Daylight Time) --- Correct in terms of current time.

const currentExecution = moment().set({
  hour: 19,
  minute: 24,
  second: 0
}).utc().valueOf();
console.log(new Date( currentTime ));//1537989840995
Prints:Wed Sep 26 2018 15:24:00 GMT-0400 (Eastern Daylight Time)


为什么显示15:24而不是19:24?
但是当我在位于EST的本地计算机上运行此代码时,它会打印
Wed Sep 26 2018 19:24:00 GMT-0400 (Eastern Daylight Time)

为什么仅在lambda中运行时会有4小时的差异?

最佳答案

由于new Date采用本地时间戳,因此它将返回您在UTC的19:07时所在的时间,即15:07。与AWS有所不同,因为它们可能不使用时区偏移,所以服务器的行为就像是在UTC中一样

您可能要使用Date.UTC ... goto

07-24 09:43
查看更多