Timestamp to human readable format

前端 未结 5 1619
面向向阳花
面向向阳花 2020-12-04 10:43

Well I have a strange problem while convert from unix timestamp to human representation using javascript

Here is timestamp

1301090400
5条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-04 11:25

    here is kooilnc's answer w/ padded 0's

    function getFormattedDate() {
        var date = new Date();
    
        var month = date.getMonth() + 1;
        var day = date.getDate();
        var hour = date.getHours();
        var min = date.getMinutes();
        var sec = date.getSeconds();
    
        month = (month < 10 ? "0" : "") + month;
        day = (day < 10 ? "0" : "") + day;
        hour = (hour < 10 ? "0" : "") + hour;
        min = (min < 10 ? "0" : "") + min;
        sec = (sec < 10 ? "0" : "") + sec;
    
        var str = date.getFullYear() + "-" + month + "-" + day + "_" +  hour + ":" + min + ":" + sec;
    
        /*alert(str);*/
    
        return str;
    }
    

提交回复
热议问题