Convert date to UTC using moment.js

后端 未结 11 845
囚心锁ツ
囚心锁ツ 2020-12-13 01:43

Probably and easy answer to this but I can\'t seem to find a way to get moment.js to return a UTC date time in milliseconds. Here is what I am doing:

var dat         


        
11条回答
  •  悲哀的现实
    2020-12-13 02:02

    As of : moment.js version 2.24.0

    let's say you have a local date input, this is the proper way to convert your dateTime or Time input to UTC :

    var utcStart = new moment("09:00", "HH:mm").utc();
    

    or in case you specify a date

    var utcStart = new moment("2019-06-24T09:00", "YYYY-MM-DDTHH:mm").utc();
    

    As you can see the result output will be returned in UTC :

    //You can call the format() that will return your UTC date in a string 
     utcStart.format(); 
    //Result : 2019-06-24T13:00:00 
    

    But if you do this as below, it will not convert to UTC :

    var myTime = new moment.utc("09:00", "HH:mm"); 
    

    You're only setting your input to utc time, it's as if your mentioning that myTime is in UTC, ....the output will be 9:00

提交回复
热议问题