Js中的一個日期處理格式化函數

來源:互聯網
上載者:User

標籤:

 1 //日期時間原型增加格式化方法 2  3 Date.prototype.Format = function (formatStr) { 4     var str = formatStr; 5     var Week = [‘日‘, ‘一‘, ‘二‘, ‘三‘, ‘四‘, ‘五‘, ‘六‘]; 6  7     str = str.replace(/yyyy|YYYY/, this.getFullYear()); 8     str = str.replace(/yy|YY/, (this.getYear() % 100) > 9 ? (this.getYear() % 100).toString() : ‘0‘ + (this.getYear() % 100)); 9     var month = this.getMonth() + 1;10     str = str.replace(/MM/, month > 9 ? month.toString() : ‘0‘ + month);11     str = str.replace(/M/g, month);12 13     str = str.replace(/w|W/g, Week[this.getDay()]);14 15     str = str.replace(/dd|DD/, this.getDate() > 9 ? this.getDate().toString() : ‘0‘ + this.getDate());16     str = str.replace(/d|D/g, this.getDate());17 18     str = str.replace(/hh|HH/, this.getHours() > 9 ? this.getHours().toString() : ‘0‘ + this.getHours());19     str = str.replace(/h|H/g, this.getHours());20     str = str.replace(/mm/, this.getMinutes() > 9 ? this.getMinutes().toString() : ‘0‘ + this.getMinutes());21     str = str.replace(/m/g, this.getMinutes());22 23     str = str.replace(/ss|SS/, this.getSeconds() > 9 ? this.getSeconds().toString() : ‘0‘ + this.getSeconds());24     str = str.replace(/s|S/g, this.getSeconds());25     return str;26 }

調用的時候比較簡單,

1 var d=new Date();2 var str=d.Format("yyyy-MM-dd  hh:mm:ss");3 console.log(str);

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.