Convert UTC date time to local date time

前端 未结 30 1154
悲哀的现实
悲哀的现实 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:40

    This is a simplified solution based on Adorjan Princ´s answer:

    function convertUTCDateToLocalDate(date) {
        var newDate = new Date(date);
        newDate.setMinutes(date.getMinutes() - date.getTimezoneOffset());
        return newDate;
    }
    

    Usage:

    var date = convertUTCDateToLocalDate(new Date(date_string_you_received));
    

提交回复
热议问题