1 function getMyTime(str,format) { 2var oDate = new Date(str*1000), // 这个看后端给你的时间戳单位是秒,还是毫秒;如果是秒,需要乘以1000 3 oYear = oDate.getFullYear(), 4 oMonth = oDate.getMonth() + 1, 5 oDay = oDate.getDate(), 6 oHour = oDate.getHours(), 7 oMin = oDate.getMinutes(), 8 oSen = oDate.getSeconds(); 9 10oTime = oYear + '-' + getzf(oMonth) + '-' + getzf(oDay)+ ' ' + getzf(oHour)+ ':' + getzf(oMin)+ ':' + getzf(oSen); //最后拼接时间11 12if(format==="Y-m-d"){13 dataTime= oTime.substr(0,10);14}else if(format==="Y-m-d-h-i"){15 dataTime= oTime.substr(0,21);16}else if(format==="Y-m-d-h-i-s"){17 dataTime= oTime;18}19return dataTime;20 }21 function getzf(num) {22if(parseInt(num) < 10) {23 num = '0' + num;24}25return num;26 }
如果觉得《unix 时间戳转化为 日期格式》对你有帮助,请点赞、收藏,并留下你的观点哦!