Convert a Unix timestamp to time in JavaScript

前端 未结 29 2090
渐次进展
渐次进展 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:32

    function getDateTime(unixTimeStamp) {
    
        var d = new Date(unixTimeStamp);
        var h = (d.getHours().toString().length == 1) ? ('0' + d.getHours()) : d.getHours();
        var m = (d.getMinutes().toString().length == 1) ? ('0' + d.getMinutes()) : d.getMinutes();
        var s = (d.getSeconds().toString().length == 1) ? ('0' + d.getSeconds()) : d.getSeconds();
    
        var time = h + '/' + m + '/' + s;
    
        return time;
    }
    
    var myTime = getDateTime(1435986900000);
    console.log(myTime); // output 01/15/00
    

提交回复
热议问题