Convert UTC Epoch to local date

后端 未结 16 1511
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-22 10:10

I have been fighting with this for a bit now. I’m trying to convert epoch to a date object. The epoch is sent to me in UTC. Whenever you pass new Date() an epoc

16条回答
  •  既然无缘
    2020-11-22 11:02

    Are you just asking to convert a UTC string to a "local" string? You could do:

    var utc_string = '2011-09-05 20:05:15';
    var local_string = (function(dtstr) {
        var t0 = new Date(dtstr);
        var t1 = Date.parse(t0.toUTCString().replace('GMT', ''));
        var t2 = (2 * t0) - t1;
        return new Date(t2).toString();
    })(utc_string);
    

提交回复
热议问题