Convert a Unix timestamp to time in JavaScript

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

    moment.js

    convert timestamps to date string in js

    https://momentjs.com/

    moment().format('YYYY-MM-DD hh:mm:ss');
    // "2020-01-10 11:55:43"
    
    moment(1578478211000).format('YYYY-MM-DD hh:mm:ss');
    // "2020-01-08 06:10:11"
    
    
    
    0 讨论(0)
  • 2020-11-21 05:20
    function timeConverter(UNIX_timestamp){
     var a = new Date(UNIX_timestamp*1000);
         var hour = a.getUTCHours();
         var min = a.getUTCMinutes();
         var sec = a.getUTCSeconds();
         var time = hour+':'+min+':'+sec ;
         return time;
     }
    
    0 讨论(0)
  • 2020-11-21 05:22

    let unix_timestamp = 1549312452
    // Create a new JavaScript Date object based on the timestamp
    // multiplied by 1000 so that the argument is in milliseconds, not seconds.
    var date = new Date(unix_timestamp * 1000);
    // Hours part from the timestamp
    var hours = date.getHours();
    // Minutes part from the timestamp
    var minutes = "0" + date.getMinutes();
    // Seconds part from the timestamp
    var seconds = "0" + date.getSeconds();
    
    // Will display time in 10:30:23 format
    var formattedTime = hours + ':' + minutes.substr(-2) + ':' + seconds.substr(-2);
    
    console.log(formattedTime);

    For more information regarding the Date object, please refer to MDN or the ECMAScript 5 specification.

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

    Based on @shomrat's answer, here is a snippet that automatically writes datetime like this (a bit similar to StackOverflow's date for answers: answered Nov 6 '16 at 11:51):

    today, 11:23
    

    or

    yersterday, 11:23
    

    or (if different but same year than today)

    6 Nov, 11:23
    

    or (if another year than today)

    6 Nov 2016, 11:23
    

    function timeConverter(t) {     
        var a = new Date(t * 1000);
        var today = new Date();
        var yesterday = new Date(Date.now() - 86400000);
        var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
        var year = a.getFullYear();
        var month = months[a.getMonth()];
        var date = a.getDate();
        var hour = a.getHours();
        var min = a.getMinutes();
        if (a.setHours(0,0,0,0) == today.setHours(0,0,0,0))
            return 'today, ' + hour + ':' + min;
        else if (a.setHours(0,0,0,0) == yesterday.setHours(0,0,0,0))
            return 'yesterday, ' + hour + ':' + min;
        else if (year == today.getFullYear())
            return date + ' ' + month + ', ' + hour + ':' + min;
        else
            return date + ' ' + month + ' ' + year + ', ' + hour + ':' + min;
    }
    
    0 讨论(0)
  • 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.

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

    In moment you must use unix timestamp:

    const dateTimeString = moment.unix(1466760005).format("DD-MM-YYYY HH:mm:ss");
    
    0 讨论(0)
提交回复
热议问题