java script 日期_利用Java Script來格式化日期

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

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

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

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

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

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

teeth_smile.gif

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

接下來我們來實作了

我實作了Date的formatdate

None.gifDate.prototype.formatDate = function(format)

ExpandedBlockStart.gif

ContractedBlock.gif    ...{    

InBlock.gif        var date = this;    

InBlock.gif        if (!format)      

InBlock.gif            format="MM/dd/yyyy";                    

InBlock.gif        var month = date.getMonth() + 1;    

InBlock.gif        var year = date.getFullYear();          

InBlock.gif        format = format.replace("MM",month.toString().padL(2,"0"));    

InBlock.gif                  

InBlock.gif        if (format.indexOf("yyyy") > -1)        

InBlock.gif            format = format.replace("yyyy",year.toString());    

InBlock.gif        else if (format.indexOf("yy") > -1)        

InBlock.gif            format = format.replace("yy",year.toString().substr(2,2));      

InBlock.gif        

InBlock.gif        format = format.replace("dd",date.getDate().toString().padL(2,"0"));      

InBlock.gif        var hours = date.getHours();            

InBlock.gif        

InBlock.gif        if (format.indexOf("t") > -1)    

ExpandedSubBlockStart.gif

ContractedSubBlock.gif        ...{        

InBlock.gif            if (hours > 11)        

InBlock.gif                format = format.replace("t","pm")        

InBlock.gif            else        

InBlock.gif                format = format.replace("t","am")    

ExpandedSubBlockEnd.gif        }    

InBlock.gif        

InBlock.gif        if (format.indexOf("HH") > -1)        

InBlock.gif            format = format.replace("HH",hours.toString().padL(2,"0"));    

InBlock.gif        

InBlock.gif        if (format.indexOf("hh") > -1)  

ExpandedSubBlockStart.gif

ContractedSubBlock.gif        ...{        

InBlock.gif            if (hours > 12) hours - 12;        

InBlock.gif            if (hours == 0) hours = 12;        

InBlock.gif            format = format.replace("hh",hours.toString().padL(2,"0"));            

ExpandedSubBlockEnd.gif        }    

InBlock.gif        

InBlock.gif        if (format.indexOf("mm") > -1)        

InBlock.gif            format = format.replace("mm",date.getMinutes().toString().padL(2,"0"));    

InBlock.gif        

InBlock.gif        if (format.indexOf("ss") > -1)        

InBlock.gif            format = format.replace("ss",date.getSeconds().toString().padL(2,"0"));    

InBlock.gif        

InBlock.gif        return format;

ExpandedBlockEnd.gif    }

接著實作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

{

str += chr

};

return str;

}

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

InBlock.gifvar date = new Date();

InBlock.gifvar str = date.formatDate("yyyy-MM-dd HH:mm:ss") + "\n" +          

InBlock.gif              date.formatDate("MM/dd/yyyy hh:mm t") + "\n" +          

InBlock.gif              date.formatDate("MM-yyyy hh:mmt") ;

InBlock.gifalert(str);

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

接著我再去實作String.Format

None.gifString.format = function(frmt,args)

ExpandedBlockStart.gif

ContractedBlock.gif...{        

InBlock.gif        for(var x=0; x

ExpandedSubBlockStart.gif

ContractedSubBlock.gif        ...{        

InBlock.gif            frmt = frmt.replace("...{"" + x + "},arguments[x+1]);    

ExpandedSubBlockEnd.gif        }    

InBlock.gif        return frmt;

ExpandedBlockEnd.gif}

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

None.gifvar date = new Date();

ExpandedBlockEnd.gif

ExpandedBlockStart.gif

ContractedBlock.gifalert(String.format("Date 1: ...{0}\nDate 2:{1}\nDate3: {2}",

None.gif                   date.formatDate("yyyy-MM-dd HH:mm"),

None.gif                   date.formatDate("MM/dd/yyyy hh:mm t"),

None.gif                   date.formatDate("MM-yyyy hh:mmt") )    

None.gif);

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值