Moment js utc 转换未按预期工作,落后于日期对象值

Moment js utc conversions not working as expected, behind date object value

我正在尝试将特定日期转换为 utc(将其保存在数据库中)并在获取后将其显示为当地时间。当我使用 moment 时,与 js Date 对象相比总是有半小时的延迟,知道为什么吗?

日期是 2016 年 5 月 8 日,浏览器时区是印度

将日期转换为 Utc: 时刻:

moment('2016-05-08', 'YYYY-MM-DD').utc().format('YYYY-MM-DD HH:MM:SS Z')
Result: "2016-05-07 18:05:00 +00:00"

日期:

new Date('2016/05/08').toUTCString()
Result: "Sat, 07 May 2016 18:30:00 GMT"

我认为 18:30 是正确答案而不是 18:05

从 Utc 至今: 时刻:

moment('2016-05-07 18:05:00 +00:00', 'YYYY-MM-DD HH:MM:SS Z').format('YYYY-MM-DDTHH:MM:SS')
Result: "2016-05-07T23:05:00" //This should be 8th May since I had started with 8th May

日期:

new Date("Sat, 07 May 2016 18:30:00 GMT").toString()
Result: "Sun May 08 2016 00:00:00 GMT+0530 (India Standard Time)" //this is the correct answer since I had initially started with 8Th May.

这一刻有什么滞后的原因吗?

您在几分钟内使用大写 M 而不是 mm,这给了您几个月的时间。换成小m就好了

moment('2016-05-08', 'YYYY-MM-DD').utc().format('YYYY-MM-DD HH:mm:ss Z')
"2016-05-07 18:30:00 +00:00"