1 function getMyTime(str,format) { 2 var 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 10 oTime = oYear + ‘-‘ + getzf(oMonth) + ‘-‘ + getzf(oDay)+ ‘ ‘ + getzf(oHour)+ ‘:‘ + getzf(oMin)+ ‘:‘ + getzf(oSen); //最后拼接时间 11 12 if(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 } 19 return dataTime; 20 } 21 function getzf(num) { 22 if(parseInt(num) < 10) { 23 num = ‘0‘ + num; 24 } 25 return num; 26 }
原文地址:https://www.cnblogs.com/ycyweb/p/9270832.html
时间: 2024-11-01 10:18:48