JS中常用的幾種時間格式處理-【筆記整理】

來源:互聯網
上載者:User

標籤:比較   class   obj   time   開始   split   date   format   string   

 

//此處整理點平時常用到的時間格式處理方法

-------------------------------------------

 1 //時間格式化函數 2 Date.prototype.format = function (format) { 3     var o = { 4         "M+": this.getMonth() + 1, //month  5         "d+": this.getDate(), //day  6         "h+": this.getHours(), //hour  7         "m+": this.getMinutes(), //minute  8         "s+": this.getSeconds(), //second  9         "q+": Math.floor((this.getMonth() + 3) / 3), //quarter 10         "S": this.getMilliseconds() //millisecond 11     }12     if (/(y+)/.test(format)) {13         format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));14     }15     for (var k in o) {16         if (new RegExp("(" + k + ")").test(format)) {17             format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));18         }19     }20     return format;21 }

 

 1 //可將  var  time = new date();轉換成 2016-08-03 18:30:00 格式//消除瀏覽器之間差異 2 function DateHandle(objDate) { 3     objDate = new Date(); //建立一個日期對象表示目前時間      4     var year = objDate.getFullYear();   //四位元字年      5     var month = objDate.getMonth() + 1; //getMonth()返回的月份是從0開始的,還要加1      6     var date = objDate.getDate(); 7     var hours = objDate.getHours(); 8     var minutes = objDate.getMinutes(); 9     var seconds = objDate.getSeconds();10     var date = year + "-" + month + "-" + date + " " + hours + ":" + minutes + ":" + seconds;11     return date;12 }
 1 //+---------------------------------------------------   2 //| 字串轉成日期類型    3 //| 格式 MM/dd/YYYY MM-dd-YYYY YYYY/MM/dd YYYY-MM-dd   4 //+---------------------------------------------------   5 function StringToDate(DateStr) { 6  7     var converted = Date.parse(DateStr); 8     var myDate = new Date(converted); 9     if (isNaN(myDate)) {10         var arys = DateStr.split(‘-‘);11         myDate = new Date(arys[0], --arys[1], arys[2]);12     }13     return myDate;14 }
 1 //+---------------------------------------------------   2 //| 比較日期差 dtEnd 格式為日期型或者有效日期格式字串   3 //+---------------------------------------------------   4 Date.prototype.DateDiff = function (strInterval, dtEnd) { 5     var dtStart = this; 6     if (typeof dtEnd == ‘string‘)//如果是字串轉換為日期型   7     { 8         dtEnd = StringToDate(dtEnd); 9     }10     switch (strInterval) {11         case ‘s‘: return parseInt((dtEnd - dtStart) / 1000);12         case ‘n‘: return parseInt((dtEnd - dtStart) / 60000);13         case ‘h‘: return parseInt((dtEnd - dtStart) / 3600000);14         case ‘d‘: return parseInt((dtEnd - dtStart) / 86400000);15         case ‘w‘: return parseInt((dtEnd - dtStart) / (86400000 * 7));16         case ‘m‘: return (dtEnd.getMonth() + 1) + ((dtEnd.getFullYear() - dtStart.getFullYear()) * 12) - (dtStart.getMonth() + 1);17         case ‘y‘: return dtEnd.getFullYear() - dtStart.getFullYear();18     }19 }
 1 //時間轉換//加數字顏色/樣式
  //time 單位 秒
2 function secondToDateAndStyle(time) { 3 time = parseInt(time); 4 if (null != time && "" != time) { 5 if (time > 60 && time < 60 * 60) { 6 time = "<span style=\"color:#FF6600;\">" + parseInt(time / 60.0) + "</span>" + "分" + "<span style=\"color:#FF6600;\">" + parseInt((parseFloat(time / 60.0) - parseInt(time / 60.0)) * 60) + "</span>" + "秒"; 7 } else if (time >= 60 * 60 && time < 60 * 60 * 24) { 8 time = "<span style=\"color:#FF6600;\">" + parseInt(time / 3600.0) + "</span>" + "時" + "<span style=\"color:#FF6600;\">" + parseInt((parseFloat(time / 3600.0) - parseInt(time / 3600.0)) * 60) + "</span>" + "分" + "<span style=\"color:#FF6600;\">" + parseInt((parseFloat((parseFloat(time / 3600.0) - parseInt(time / 3600.0)) * 60) - parseInt((parseFloat(time / 3600.0) - parseInt(time / 3600.0)) * 60)) * 60) + "</span>" + "秒"; 9 } else {10 time = "<span style=\"color:#FF6600;\">" + parseInt(time) + "</span>" + "秒";11 }12 } else {13 time = "<span style=\"color:#FF6600;\">0</span>秒";14 }15 return time;16 }

持續整理添加中

待續...

 

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.