`
hwaspf
  • 浏览: 8034 次
  • 性别: Icon_minigender_1
  • 来自: 长沙
最近访客 更多访客>>
社区版块
存档分类
最新评论

时间格式转换

阅读更多
<html>    
<head>   
<title> test </title>    
<script language="javascript">      
    function ChangeDateToString(DateIn){
	    var Year=0;
	    var Month=0;
	    var Day=0;
	    var Hours=0;
	    var Minutes=0;
	    var Seconds=0;
	
	    var CurrentDate="";
	
	    //初始化时间
	    Year      = DateIn.getFullYear();
	    Month     = DateIn.getMonth()+1;
	    Day       = DateIn.getDate();
	    Hours=DateIn.getHours();
	    Minutes=DateIn.getMinutes();
	    Seconds=DateIn.getSeconds();
	
	    CurrentDate = Year;
	    if (Month >= 10 ){
	        CurrentDate = CurrentDate + Month;
	    }else{
	        CurrentDate = CurrentDate + "0" + Month;
	    }
	    
	    if (Day >= 10 ){
	        CurrentDate = CurrentDate + Day ;
	    }
	    else{
	        CurrentDate = CurrentDate + "0" + Day ;
	    }
	    
	    if(Hours>=10){
	    	CurrentDate=CurrentDate+Hours;
	    }else{
	    	CurrentDate = CurrentDate + "0" + Hours ;
	    }
	    
		if(Minutes>=10){
	    	CurrentDate=CurrentDate+Minutes;
	    }else{
	    	CurrentDate = CurrentDate + "0" + Minutes ;
	    }
	    
	    if(Seconds>=10){
	    	CurrentDate=CurrentDate+Seconds;
	    }else{
	    	CurrentDate = CurrentDate + "0" + Seconds ;
	    }
	    return CurrentDate;
    }
       

    function test() {   
        var testDate = new Date();   
        alert(testDate);   
        var testStr = testDate.format("yyyy-MM-dd hh:mm:ss");   
        alert(testStr);   
    }   

    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;   
    }   
</script>    
</head>    
<body>      
<a href="#" onclick="test()">test</a>   
</body>    
</html>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics