日期格式化

    科技2022-07-16  116

    js 格式化时间戳,显示格式 yyyy-MM-dd HH:mm:ss 。输入的 timestamp 例如 1510452672000

    function formatTimestamp(timestamp) { console.log(timestamp) var dateObj = new Date(timestamp); var year = dateObj.getYear() + 1900; var month = dateObj.getMonth() + 1; var theDate = dateObj.getDate(); var hour = dateObj.getHours(); var minute = dateObj.getMinutes(); var second = dateObj.getSeconds(); return year + "-" + month + "-" + theDate + " " + hour + ":" + minute + ":" + second; } function dateformatters(time) { if (time && time > 0) { var date = new Date(time);//这里必须是整数,毫秒 var seperator1 = "-"; var month = date.getMonth() + 1; var strDate = date.getDate(); //默认为当日 if (month >= 1 && month <= 9) { month = "0" + month; } if (strDate >= 0 && strDate <= 9) { strDate = "0" + strDate; } var testStr = date.getFullYear() + "-" + month + "-" + strDate + " " + (date.getHours() <= 9 ? "0" + date.getHours() : date .getHours()) + ":" + (date.getMinutes() <= 9 ? "0" + date.getMinutes() : date.getMinutes()) + ":" + (date.getSeconds() <= 9 ? "0" + date.getSeconds() : date.getSeconds()) return testStr; } else { return ""; } }
    Processed: 0.009, SQL: 8