js 转换时间戳为时间格式并且按指定格式输出
/** * 时间戳转换为日期 */ function convertTimestamp(timestamp){ // 时间戳转换为日期 var d = new Date(timestamp); // 日期按指定格式输出(例如: 2018-09-03 17:09:45) var year = date.getFullYear(); var month = date.getMonth() + 1; var day = date.getDate(); var hour = date.getHours(); var minute = date.getMinutes(); var second = date.getSeconds(); month = (month<10)?"0"+month:month; day = (day <10)?"0"+day :day ; hour = (hour <10)?"0"+hour :hour ; minute = (minute <10)?"0"+minute :minute ; second= (second<10)?"0"+second:second; // 结果返回 return year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second } // 测试 convertTimestamp(1535967523000); /** * 日期转换为时间戳 */ function convertDate(date){ return Date.parse(date); } // 测试 convertDate(new Date());