Convert a Unix timestamp to time in JavaScript

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

    // Format value as two digits 0 => 00, 1 => 01
    function twoDigits(value) {
       if(value < 10) {
        return '0' + value;
       }
       return value;
    }
    
    var date = new Date(unix_timestamp*1000);
    // display in format HH:MM:SS
    var formattedTime = twoDigits(date.getHours()) 
          + ':' + twoDigits(date.getMinutes()) 
          + ':' + twoDigits(date.getSeconds());
    

提交回复
热议问题