利用Java Script來格式化日期

摘要:利用Java Script來格式化日期

在.net世界裡,如果我要格式化日期的話

我只要用tostring的方式就可以了

如果是VB.NET,也可以用FormatDateTime

可是在javascript ,難道就只能慢慢拆解嗎@@

經過我的努力不懈  終於讓我找到了方法

首先要去了解什麼是prototype在javascript扮演的是什麼角色

javascript中的prototype物件是什麼

 

接下來我們來實作了

我實作了Date的formatdate

Date.prototype.formatDate = function(format)
    {    
        var date = this;    
        if (!format)      
            format="MM/dd/yyyy";                    
        var month = date.getMonth() + 1;    
        var year = date.getFullYear();          
        format = format.replace("MM",month.toString().padL(2,"0"));    
                  
        if (format.indexOf("yyyy") > -1)        
            format = format.replace("yyyy",year.toString());    
        else if (format.indexOf("yy") > -1)        
            format = format.replace("yy",year.toString().substr(2,2));      
        
        format = format.replace("dd",date.getDate().toString().padL(2,"0"));      
        var hours = date.getHours();            
        
        if (format.indexOf("t") > -1)    
        {        
            if (hours > 11)        
                format = format.replace("t","pm")        
            else        
                format = format.replace("t","am")    
        }
    
        
        if (format.indexOf("HH") > -1)        
            format = format.replace("HH",hours.toString().padL(2,"0"));    
        
        if (format.indexOf("hh") > -1)  
        {        
            if (hours > 12) hours - 12;        
            if (hours == 0) hours = 12;        
            format = format.replace("hh",hours.toString().padL(2,"0"));            
        }
    
        
        if (format.indexOf("mm") > -1)        
            format = format.replace("mm",date.getMinutes().toString().padL(2,"0"));    
        
        if (format.indexOf("ss") > -1)        
            format = format.replace("ss",date.getSeconds().toString().padL(2,"0"));    
        
        return format;
    }

接著實作String的padLeft與padRight

String.prototype.padL = function(width,pad)
    {    
        if (!width ||width<1)        
            return this;        
        
        if (!pad) pad=" ";            
        
        var length = width - this.length    
        
        if (length < 1)  
            return this.substr(0,width);      
        
        return (String.repeat(pad,length) + this).substr(0,width);    
    }    
    String.prototype.padR = function(width,pad)
    {    
       if (!width || width<1)        
           return this;              
        
       if (!pad) pad=" ";    
        
       var length = width - this.length    
        
       if (length < 1) this.substr(0,width);      
           return (this + String.repeat(pad,length)).substr(0,width);
    }  
   String.repeat = function(chr,count)
    {        
        var str = "";      
        for(var x=0;x<count;x++)  
        {
            str += chr
        };      
        return str;
    }

到這邊我們就可以呼叫Date.formatDate來玩了

var date = new Date();
var str = date.formatDate("yyyy-MM-dd HH:mm:ss") + "\n" +          
              date.formatDate("MM/dd/yyyy hh:mm t") + "\n" +          
              date.formatDate("MM-yyyy hh:mmt") ;
alert(str);  

想不到javascript來處理日期是這麼麻煩   難怪我很不想寫javascript

接著我再去實作String.Format

String.format = function(frmt,args)
{        
        for(var x=0; x<arguments.length; x++)    
        {        
            frmt = frmt.replace("{"" + x + "},arguments[x+1]);    
        }
    
        return frmt;
}

一樣,我們這邊只要呼叫即可馬上使用

var date = new Date();
alert(String.format("Date 1: {0}\nDate 2:{1}\nDate3: {2}",
                   date.formatDate("yyyy-MM-dd HH:mm"),
                   date.formatDate("MM/dd/yyyy hh:mm t"),
                   date.formatDate("MM-yyyy hh:mmt") )    
);

 

 

 

上述提供的程式與操作,僅供大家參考。