MomentJS getting JavaScript Date in UTC

后端 未结 3 1530
春和景丽
春和景丽 2020-12-01 04:26

I am not able to get the JavaScript Date string for MongoDB record via the following. It keeps using my local time.

var utc = moment.utc().valueOf();
console         


        
3条回答
  •  孤独总比滥情好
    2020-12-01 04:55

    Calling toDate will create a copy (the documentation is down-right wrong about it not being a copy), of the underlying JS Date object. JS Date object is stored in UTC and will always print to eastern time. Without getting into whether .utc() modifies the underlying object that moment wraps use the code below.

    You don't need moment for this.

    new Date().getTime()
    

    This works, because JS Date at its core is in UTC from the Unix Epoch. It's extraordinarily confusing and I believe a big flaw in the interface to mix local and UTC times like this with no descriptions in the methods.

提交回复
热议问题