用js完成毫秒格式資料的日期格式化任務

來源:互聯網
上載者:User

標籤:min   turn   使用方法   ons   cti   任務   second   author   ring   

後台傳過來的資料  creationTime  在後台是Date類型的

毫秒轉換成  05-24  月 日格式的

  1. //獲得月日得到日期oTime  
  2.         function getMoth(str){  
  3.             var oDate = new Date(str),  
  4.             oMonth = oDate.getMonth()+1,  
  5.             oDay = oDate.getDate(),  
  6.             oTime = getzf(oMonth) +‘-‘+ getzf(oDay);//最後拼接時間  
  7.             return oTime;  
  8.         };  
  9.         console.log(getMoth(1465959000));//使用方法 

 

毫秒轉換成 年月日+時分秒 格式的  1970-01-18 07:12:39   

補0操作: 比如2012-2-2  就會變成    2012-02-02

使用方法: getMyDate(data[i].creationTime);

 

  1. //獲得年月日      得到日期oTime  
  2.         function getMyDate(str){  
  3.             var oDate = new Date(str),  
  4.             oYear = oDate.getFullYear(),  
  5.             oMonth = oDate.getMonth()+1,  
  6.             oDay = oDate.getDate(),  
  7.             oHour = oDate.getHours(),  
  8.             oMin = oDate.getMinutes(),  
  9.             oSen = oDate.getSeconds(),  
  10.             oTime = oYear +‘-‘+ getzf(oMonth) +‘-‘+ getzf(oDay) +‘ ‘+ getzf(oHour) +‘:‘+ getzf(oMin) +‘:‘+getzf(oSen);//最後拼接時間  
  11.             return oTime;  
  12.         };  
  13.         //補0操作  
  14.         function getzf(num){  
  15.             if(parseInt(num) < 10){  
  16.                 num = ‘0‘+num;  
  17.             }  
  18.             return num;  
  19.         }  

毫秒轉換成  年月日 時分秒的格式

  1. /*  
  2.         js由毫秒數得到年月日  
  3.         使用: (new Date(data[i].creationTime)).Format("yyyy-MM-dd hh:mm:ss.S")  
  4.         */  
  5.         Date.prototype.Format = function (fmt) { //author: meizz  
  6.             var o = {  
  7.                 "M+": this.getMonth() + 1, //月份  
  8.                 "d+": this.getDate(), //日  
  9.                 "h+": this.getHours(), //小時  
  10.                 "m+": this.getMinutes(), //分  
  11.                 "s+": this.getSeconds(), //秒  
  12.                 "q+": Math.floor((this.getMonth() + 3) / 3), //季度  
  13.                 "S": this.getMilliseconds() //毫秒  
  14.             };  
  15.             if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));  
  16.             for (var k in o)  
  17.                 if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));  
  18.             return fmt;  
  19.         };  

用js完成毫秒格式資料的日期格式化任務

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.