Any way to parse a time string using Moment.js but ignore timezone info?

前端 未结 10 1643
深忆病人
深忆病人 2020-12-30 01:56

Given the volume of Timezone questions, I would have thought to be able to find the answer to this issue, but haven\'t had any success.

Is there a way using mo

10条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-30 02:27

    I don't think you really want to ignore the offset. That would ultimately just be replacing the offset you provided with one from your local time zone - and that would result in a completely different moment in time.

    Perhaps you are just looking for a way to have a moment retain the time zone it was given? If so, then use the moment.parseZone function. For example:

    var m = moment.parseZone("2012-12-31T00:00:00+0000");
    var s = m.format();   // "2012-12-31T00:00:00+00:00"
    

    You could also achieve this with moment.utc. The difference is that moment.parseZone will retain whatever offset you give it, while moment.utc will adjust to UTC if you give it a non-zero offset.

提交回复
热议问题