Convert a Unix timestamp to time in JavaScript

前端 未结 29 2075
渐次进展
渐次进展 2020-11-21 04:57

I am storing time in a MySQL database as a Unix timestamp and that gets sent to some JavaScript code. How would I get just the time out of it?

For example, in HH/MM/

29条回答
  •  执笔经年
    2020-11-21 05:22

    The answer given by @Aron works, but it didn't work for me as I was trying to convert timestamp starting from 1980. So I made few changes as follows

    function ConvertUnixTimeToDateForLeap(UNIX_Timestamp) {
        var dateObj = new Date(1980, 0, 1, 0, 0, 0, 0);
        dateObj.setSeconds(dateObj.getSeconds() + UNIX_Timestamp);
        return dateObj;  
    }
    
    document.body.innerHTML = 'TimeStamp : ' + ConvertUnixTimeToDateForLeap(1269700200);

    So if you have a timestamp starting from another decade or so, just use this. It saved a lot of headache for me.

提交回复
热议问题