JavaScript-日期格式化(一)


    /**  
      * 日期格式化,  
      * @param date 要格式化的日期  
      * @param pattern 进行格式化的模式  
      *     支持的模式字母有:  
      *     y:年,  
      *     M:年中的月份(1-12),  
      *     d:月份中的天(1-31),  
      *     H:小时(0-23),  
      *     h:小时(0-12),  
      *     m:分(0-59),  
      *     s:秒(0-59),  
      *     S:毫秒(0-999),  
      *     E:星期(以汉语表示),  
      *     e:星期(以英文表示),  
      *     A:上午/下午标识,  
      *     a:AM/PM标识  
      * @return  
      */  
    function formatDate(date,pattern){   
         var d;   
         if((d=parseDate(date))==null){   
             return "";   
         }   
         if(!pattern){   
             pattern = "yyyy-MM-dd";   
         }   
         var arrWeek = ["星期日","星期一","星期二","星期三","星期四","星期五","星期六","Sunday","Monday","Tuesday","Tuesday","Thursday","Friday","Saturday"];   
         var value = new Object();   
         value["y"] = parseString(date.getFullYear());   
         value["M"] = parseString(date.getMonth() + 1);   
         value["d"] = parseString(date.getDate());   
         value["H"] = parseString(date.getHours());   
         value["h"] = parseString(value["H"] > 12 ? (value["H"]-12) : value["H"]);   
         value["m"] = parseString(date.getMinutes());   
         value["s"] = parseString(date.getSeconds());   
         value["S"] = parseString(date.getMilliseconds());   
         value["E"] = arrWeek[date.getDay()];   
         value["e"] = arrWeek[date.getDay() + 7];   
         value["a"] = (value["H"] > 12 ? "PM" : "AM");   
         value["A"] = (value["H"] > 12 ? "下午" : "上午");   
         var result = "";   
         var i = 0;   
         var hasE = false;//是否出现过星期   
         var hasAMPM = false;//是否出现过上午下午   
         while(i < pattern.length){   
             var c = pattern.charAt(i++);   
             var lc = c;//记录本次要处理的字母,如'y'   
             var tmpStr = c;//本次在处理的字母格式,如'yyyy'   
             while(i < pattern.length && (c=pattern.charAt(i))==lc){   
                 tmpStr += c;   
                 i++;   
             }   
             if(value[lc]!=""&&value[lc]!=null&&value[lc]!="undefined"){   
                 //本次要处理的字母是模式母   
                 if((lc == "E" || lc == "e") && !hasE){   
                     //星期   
                     result += value[lc];   
                     hasE = true;   
                 } else if(lc == "E" || lc == "e") {   
                     result += tmpStr;   
                 } else if((lc=="a" || lc == "A")  && !hasAMPM){   
                     //上下午   
                     result += value[lc];   
                     hasAMPM = true;   
                 } else if((lc=="a" || lc == "A") ){   
                     result += tmpStr;   
                 } else {   
                     //如果是 单个的日期,月份,小时,分,秒的字符串,不能再进行字符串的截取操作   
                     if(tmpStr == "d" || tmpStr == "M" || tmpStr=="H" || tmpStr=="h" || tmpStr == "m" || tmpStr == "s"){  
                         result += value[lc];   
                     } else {   
                         result += value[lc].fillChar(tmpStr.length);   
                     }   
                 }   
             } else {//非模式字母,直接输出   
                 result += tmpStr;   
             }   
         }   
         return result;   
     }  

测试:

var date = new Date();   
document.write(formatDate(date,"yyyy-MM-dd HH:mm:ss.SSS E") + "<br>");   
document.write(formatDate(date,"yyyy年MM月dd日 HH时mm分ss秒 E") + "<br>");   
document.write(formatDate(date,"yyyy-M-d hh:mm:ss.SSS A E") + "<br>");   
document.write(formatDate(date,"MM/dd/yy hh:mm:ss a e") + "<br>");   
document.write(formatDate(date,"M/d/yyyy") + "<br>");   
posted @ 2015-12-22 21:03  贺佃伟  阅读(113)  评论(0编辑  收藏  举报