Convert UTC date time to local date time

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

    I wrote a nice little script that takes a UTC epoch and converts it the client system timezone and returns it in d/m/Y H:i:s (like the PHP date function) format:

    getTimezoneDate = function ( e ) {
    
        function p(s) { return (s < 10) ? '0' + s : s; }        
    
        var t = new Date(0);
        t.setUTCSeconds(e);
    
        var d = p(t.getDate()), 
            m = p(t.getMonth()+1), 
            Y = p(t.getFullYear()),
            H = p(t.getHours()), 
            i = p(t.getMinutes()), 
            s = p(t.getSeconds());
    
        d =  [d, m, Y].join('/') + ' ' + [H, i, s].join(':');
    
        return d;
    
    };
    

提交回复
热议问题