Convert a Unix timestamp to time in JavaScript

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

    Another way - from an ISO 8601 date.

    var timestamp = 1293683278;
    var date = new Date(timestamp * 1000);
    var iso = date.toISOString().match(/(\d{2}:\d{2}:\d{2})/)
    alert(iso[1]);

    0 讨论(0)
  • 2020-11-21 05:39

    You can use the following function to convert your timestamp to HH:MM:SS format :

    var convertTime = function(timestamp, separator) {
        var pad = function(input) {return input < 10 ? "0" + input : input;};
        var date = timestamp ? new Date(timestamp * 1000) : new Date();
        return [
            pad(date.getHours()),
            pad(date.getMinutes()),
            pad(date.getSeconds())
        ].join(typeof separator !== 'undefined' ?  separator : ':' );
    }
    

    Without passing a separator, it uses : as the (default) separator :

    time = convertTime(1061351153); // --> OUTPUT = 05:45:53
    

    If you want to use / as a separator, just pass it as the second parameter:

    time = convertTime(920535115, '/'); // --> OUTPUT = 09/11/55
    

    Demo

    var convertTime = function(timestamp, separator) {
        var pad = function(input) {return input < 10 ? "0" + input : input;};
        var date = timestamp ? new Date(timestamp * 1000) : new Date();
        return [
            pad(date.getHours()),
            pad(date.getMinutes()),
            pad(date.getSeconds())
        ].join(typeof separator !== 'undefined' ?  separator : ':' );
    }
    
    document.body.innerHTML = '<pre>' + JSON.stringify({
        920535115 : convertTime(920535115, '/'),
        1061351153 : convertTime(1061351153, ':'),
        1435651350 : convertTime(1435651350, '-'),
        1487938926 : convertTime(1487938926),
        1555135551 : convertTime(1555135551, '.')
    }, null, '\t') +  '</pre>';

    See also this Fiddle.

    0 讨论(0)
  • 2020-11-21 05:40

    See Date/Epoch Converter.

    You need to ParseInt, otherwise it wouldn't work:


    if (!window.a)
        window.a = new Date();
    
    var mEpoch = parseInt(UNIX_timestamp);
    
    if (mEpoch < 10000000000)
        mEpoch *= 1000;
    
    ------
    a.setTime(mEpoch);
    var year = a.getFullYear();
    ...
    return time;
    
    0 讨论(0)
  • 2020-11-21 05:40

    function getDateTimeFromTimestamp(unixTimeStamp) {
        let date = new Date(unixTimeStamp);
        return ('0' + date.getDate()).slice(-2) + '/' + ('0' + (date.getMonth() + 1)).slice(-2) + '/' + date.getFullYear() + ' ' + ('0' + date.getHours()).slice(-2) + ':' + ('0' + date.getMinutes()).slice(-2);
    }
    
    const myTime = getDateTimeFromTimestamp(1435986900000);
    console.log(myTime); // output 01/05/2000 11:00

    0 讨论(0)
  • 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());
    
    0 讨论(0)
提交回复
热议问题