JS常用的方法

來源:互聯網
上載者:User

標籤:

1.時間戳記轉換

 1 //時間戳記(有Date和無Date的都可)轉換為日期 “2016年5月30日 10:29:30   2016-05-20 09:11” 2 function TimeConversion(timeStamp,type) { 3     if (timeStamp) { 4         if (timeStamp.indexOf(‘Date‘) > -1) { 5             var dtime = timeStamp.substring(6, timeStamp.length - 2); 6             var newDate = new Date(); 7             newDate.setTime(dtime); 8             var year = newDate.getFullYear(); 9             var month = newDate.getMonth() + 1;10             var data = newDate.getDate();11             var hour = ‘0‘ + newDate.getHours();12             hour = hour.substring(hour.length - 2)13             var minute = ‘0‘ + newDate.getMinutes();14             minute = minute.substring(minute.length - 2)15             var second = ‘0‘ + newDate.getSeconds();16             second = second.substring(second.length - 2)17             var s = "";18             if (type && type == 1) {19                 s = year + "-" + month + "-" + data + "  " + hour + ":" + minute;20             }21             else {22                 s = year + "年" + month + "月" + data + "日  " + hour + ":" + minute + ":" + second;23             }24 25             return s;26         }27         else {28             var dt = timeStamp.split(‘ ‘);29             var s = dt;30             if (dt.length > 1) {31                 var date = dt[0].split(‘/‘);32                 var time = dt[1].split(‘:‘);33                 var hour = ‘0‘ + time[0];34                 hour = hour.substring(hour.length - 2);35                 if (type && type == 1) {36                     s = date[0] + "-" + date[1] + "-" + date[2] + "  " + hour + ":" + time[1];37                 }38                 else {39                     s = date[0] + "年" + date[1] + "月" + date[2] + "日  " + hour + ":" + time[1] + ":" + time[2];40                 }41             }42             return s;43         }44     }45 }

 

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.