js timestamp 转换 date 和 将秒数整理为时分秒格式
// 获得的后台json 时间格式转化 例如:1520305366000 转化为XXXX-XX-XX类似这种
1 function timeStamp2String(time){ 2 var datetime = new Date(); 3 datetime.setTime(time); 4 var year = datetime.getFullYear(); 5 var month = datetime.getMonth() + 1 < 10 ? "0" + (datetime.getMonth() + 1) : datetime.getMonth() + 1; 6 var date = datetime.getDate() < 10 ? "0" + datetime.getDate() : datetime.getDate(); 7 var hour = datetime.getHours()< 10 ? "0" + datetime.getHours() : datetime.getHours(); 8 var minute = datetime.getMinutes()< 10 ? "0" + datetime.getMinutes() : datetime.getMinutes(); 9 var second = datetime.getSeconds()< 10 ? "0" + datetime.getSeconds() : datetime.getSeconds(); 10 return year + "-" + month + "-" + date+“-”+hours+“-”+minutes+“-”+seconds; 11 }
//js转化获取的秒数 , 为06:23,用于时钟、音视频播放。
1 function formatSeconds(value) { 2 var theTime = parseInt(value);// 秒 3 var theTime1 = 0;// 分 4 var theTime2 = 0;// 小时 5 if (theTime > 60) { 6 theTime1 = parseInt(theTime / 60); 7 theTime = parseInt(theTime % 60); 8 if (theTime1 > 60) { 9 theTime2 = parseInt(theTime1 / 60); 10 theTime1 = parseInt(theTime1 % 60); 11 } 12 } 13 14 var result = "" + parseInt(theTime);//秒 15 if (10 > theTime > 0) { 16 result = "0" + parseInt(theTime);//秒 17 } else { 18 result = "" + parseInt(theTime);//秒 19 } 20 21 if (10 > theTime1 > 0) { 22 result = "0" + parseInt(theTime1) + ":" + result;//分,不足两位数,首位补充0, 23 } else { 24 result = "" + parseInt(theTime1) + ":" + result;//分 25 } 26 if (theTime2 > 0) { 27 result = "" + parseInt(theTime2) + ":" + result;//时 28 } 29 return result; 30 31 }
如有侵权,请联系