Convert UTC date time to local date time

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

    In Angular I used Ben's answer this way:

    $scope.convert = function (thedate) {
        var tempstr = thedate.toString();
        var newstr = tempstr.toString().replace(/GMT.*/g, "");
        newstr = newstr + " UTC";
        return new Date(newstr);
    };
    

    Edit: Angular 1.3.0 added UTC support to date filter, I haven't use it yet but it should be easier, here is the format:

    {{ date_expression | date : format : timezone}}
    

    Angular 1.4.3 Date API

提交回复
热议问题