zoukankan      html  css  js  c++  java
  • 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>");   
  • 相关阅读:
    Codeforces Round #313 (Div. 1) A.Gerald's Hexagon
    COJN 0585 800604鸡蛋的硬度
    COJN 0584 800603吃糖果
    COJN 0583 800602分苹果
    COJN 0575 800601滑雪
    昨天的补记
    重构的代码
    写了一个复杂的sql语句
    一个想法
    安装了C
  • 原文地址:https://www.cnblogs.com/hedianwei/p/5671365.html
Copyright © 2011-2022 走看看