Moment.js Convert Local time to UTC time does work

后端 未结 7 1860
伪装坚强ぢ
伪装坚强ぢ 2021-01-01 18:23

I would like to use Moment.js to convert a local time to UTC equivalent. I believe that I have the correct method in place, but it does not alter the time.

I\'m in S

7条回答
  •  清酒与你
    2021-01-01 19:00

    Create a local moment object from you local time and convert it to UTC then format it, then create a new UTC moment from that formatted UTC string

    var localDateString = '24/04/2019';
    var localDateStringFormat = 'DD/MM/YYYY';
    
    var utcMoment = moment.utc(moment(localDateString, localDateStringFormat ).utc().format('YYYY-MM-DD HH:mm:ssZ'))
    
    console.log(utcMoment);

提交回复
热议问题