Convert date to UTC using moment.js

后端 未结 11 824
囚心锁ツ
囚心锁ツ 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:04

    here, I'm passing the date object and converting it into UTC time.

    $.fn.convertTimeToUTC = function (convertTime) {
       if($(this).isObject(convertTime)) {
            return moment.tz(convertTime.format("Y-MM-DD HH:mm:ss"), moment.tz.guess()).utc().format("Y-MM-DD HH:mm:ss");
        }
    };
    // Returns if a value is an object
    $.fn.isObject =  function(value) {
        return value && typeof value === 'object';
    };
    
    
    //you can call it as below
    $(this).convertTimeToUTC(date);
    

提交回复
热议问题