對 meizz javascript date format 日期格式化函數的增強和改寫

來源:互聯網
上載者:User

meizz的javascript日期格式化函數很好用

原帖請見:http://blog.csdn.net/meizz/article/details/405708

Date.prototype.format = function(format) //author: meizz{  var o = {    "M+" : this.getMonth()+1, //month    "d+" : this.getDate(),    //day    "h+" : this.getHours(),   //hour    "m+" : this.getMinutes(), //minute    "s+" : this.getSeconds(), //second    "q+" : Math.floor((this.getMonth()+3)/3),  //quarter    "S" : this.getMilliseconds() //millisecond  }  if(/(y+)/.test(format)) format=format.replace(RegExp.$1,    (this.getFullYear()+"").substr(4 - RegExp.$1.length));  for(var k in o)if(new RegExp("("+ k +")").test(format))    format = format.replace(RegExp.$1,      RegExp.$1.length==1 ? o[k] :         ("00"+ o[k]).substr((""+ o[k]).length));  return format;}alert(new Date().format("yyyy-MM-dd hh:mm:ss"));

代碼寫得很精妙,主要使用了正則和replace,我簡單地做了一些增強和改寫,

主要用了replace 函數來代替RegExp.$1

增加了24小時標記法的H

原來的h變成12小時制

增加了E代表星期,當然可以再擴充

/*字母  日期或時間元素  表示  樣本  G  Era 標誌符  Text  AD  y  年  Year  1996; 96  M  年中的月份  Month  July; Jul; 07  w  年中的周數  Number  27  W  月份中的周數  Number  2  D  年中的天數  Number  189  d  月份中的天數  Number  10  F  月份中的星期  Number  2  E  星期中的天數  Text  Tuesday; Tue  a  Am/pm 標記  Text  PM  H  一天中的小時數(0-23)  Number  0  k  一天中的小時數(1-24)  Number  24  K  am/pm 中的小時數(0-11)  Number  0  h  am/pm 中的小時數(1-12)  Number  12  m  小時中的分鐘數  Number  30  s  分鐘中的秒數  Number  55  S  毫秒數  Number  978  z  時區  General time zone  Pacific Standard Time; PST; GMT-08:00  Z  時區  RFC 822 time zone  -0800  */String.prototype.repeat = function(count, seperator) {var seperator = seperator || '';var a = new Array(count);for (var i = 0; i < count; i++){a[i] = this;}return a.join(seperator);}Date.prototype.format = function(style) {var o = {"y{4}|y{2}" : this.getFullYear(), //year"M{1,2}" : this.getMonth() + 1, //month"d{1,2}" : this.getDate(),      //day"H{1,2}" : this.getHours(),     //hour"h{1,2}" : this.getHours()  % 12,  //hour"m{1,2}" : this.getMinutes(),   //minute"s{1,2}" : this.getSeconds(),   //second"E" : this.getDay(),   //day in week"q" : Math.floor((this.getMonth() + 3) / 3),  //quarter"S{3}|S{1}"  : this.getMilliseconds() //millisecond}for(var k in o ){style = style.replace(new RegExp("("+ k +")"), function(m){return ("0".repeat(m.length) + o[k]).substr(("" + o[k]).length);})}return style;};document.write(new Date().format('yyyy-MM-dd Qq WE h:mm:ss SSS'));

代碼貌似少了一點。

replace函數的功能很強大,從下面這個簡單的常式中學習一下:

String.prototype.format = function(){    var args = arguments;    return this.replace(/\{(\d+)\}/g,                        function(m,i){            return args[i];        });}var a = "I Love {0}, and You Love {1},Where are {0}! {4}";alert(a.format("You","Me"));

stringObj.replace參數

第一個參數為正則

第二個參數可以是文本,這個很簡單,也可以是function,這個就強大了。

這個function 的參數是動態,有多少個參數呢,m+3呢,m是多少呢,是正則中左括弧的個數,如果有一個左括弧,那麼就有1+3=4個參數

所以最少有3個參數了。

參數1:匹配的子字串

參數2:尋找中捕獲的全部結果(往往和第一個相同,如果()在最外面的話)

參數3:尋找中捕獲的全部結果

..........:尋找中捕獲的全部結果

參數m+2:在 stringObj 中匹配出現的位移量

參數m+3:stringObj 本身

這麼一講,很通俗易懂了。

更新:2012/12/13,替換混淆字串:

var o = "sina"; //原字串var re = /s(.+)i(.+)n(.+)a(.+)/g; //原字串正則var s = "svserinfonoaction"; //混淆後的字串//alert(re.test(s));var r = s.replace(re,     function(){    var t = arguments[0];    for (var i = 0; i < o.length; i++) {        t = t.replace(arguments[i + 1], "");     }    return t;    });document.write(r);

這兒有更詳細的參考:

http://www.blogjava.net/pingpang/archive/2012/08/12/385342.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.