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 }

 

posted @ 2018-03-12 15:06  琼琼Daring  阅读(2826)  评论(0编辑  收藏  举报