`
royzhou1985
  • 浏览: 249763 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

javascript格式化日期

阅读更多
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">  
<HTML>  
<HEAD>  
 <TITLE> New Document </TITLE>  
 <META NAME="Generator" CONTENT="EditPlus">  
 <META NAME="Author" CONTENT="">  
 <META NAME="Keywords" CONTENT="">  
 <META NAME="Description" CONTENT="">  
 <SCRIPT LANGUAGE="JavaScript">  
  <!--   
    Date.prototype.format = function(format) {      
        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;      
    }      
    String.prototype.toDate = function(style) {   
        if (style == null)    
            style = 'yyyy-MM-dd hh:mm:ss';   
        var o = {   
            'y+' : 'y',   
            'M+' : 'M',   
            'd+' : 'd',   
            'h+' : 'h',   
            'm+' : 'm',   
            's+' : 's'   
        };   
        var result = {   
            'y' : '',   
            'M' : '',   
            'd' : '',   
            'h' : '00',   
            'm' : '00',   
            's' : '00'   
        }   
        var tmp = style;   
        for (var k in o) {   
            if (new RegExp('(' + k + ')').test(style)) {   
                result[o[k]] = this.substring(tmp.indexOf(RegExp.$1), tmp.indexOf(RegExp.$1) + RegExp.$1.length);   
            }   
        }   
        return new Date(result['y'], result['M'] - 1, result['d'], result['h'], result['m'], result['s']);   
    };   
    var current = "2009-10-12".toDate("yyyy-MM-dd");
	alert(current);
   alert(current.format("yyyy年MM月dd日"));   
  //-->  
  </SCRIPT>  
 </HEAD>  
 <BODY>  
 </BODY>  
</HTML>  


JS中日期与字符串之间的相互转换
其中RegExp.$1表示第1组匹配的结果,这个在正则表达式里有多个以()分组时有用

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics