Convert UTC date time to local date time

前端 未结 30 1174
悲哀的现实
悲哀的现实 2020-11-22 01:09

From the server I get a datetime variable in this format: 6/29/2011 4:52:48 PM and it is in UTC time. I want to convert it to the current user’s browser time us

30条回答
  •  悲&欢浪女
    2020-11-22 01:31

    After trying a few others posted here without good results, this seemed to work for me:

    convertUTCDateToLocalDate: function (date) {
        return new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate(),  date.getHours(), date.getMinutes(), date.getSeconds()));
    }
    

    And this works to go the opposite way, from Local Date to UTC:

    convertLocalDatetoUTCDate: function(date){
        return new Date(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(),  date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds());
    }
    

提交回复
热议问题