javascript Date format(js日期格式化)
01.//对Date的扩展,将Date转化为指定格式的String
02.//月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q)可以用1-2个占位符,
03.//年(y)可以用1-4个占位符,毫秒(S)只能用1个占位符(是1-3位的数字)
04.//例子:
05.//(newDate()).Format("yyyy-MM-ddhh:mm:ss.S")==>2006-07-0208:09:04.423
06.//(newDate()).Format("yyyy-M-dh:m:s.S")==>2006-7-28:9:4.18
07.Date.prototype.Format=function(fmt)
08.{//author:meizz
09.varo={
10."M+":this.getMonth()+1,//月份
11."d+":this.getDate(),//日
12."h+":this.getHours(),//小时
13."m+":this.getMinutes(),//分
14."s+":this.getSeconds(),//秒
15."q+":Math.floor((this.getMonth()+3)/3),//季度
16."S":this.getMilliseconds()//毫秒
17.};
18.if(/(y+)/.test(fmt))
19.fmt=fmt.replace(RegExp.$1,(this.getFullYear()+"").substr(4-RegExp.$1.length));
20.for(varkino)
21.if(newRegExp("("+k+")").test(fmt))
22.fmt=fmt.replace(RegExp.$1,(RegExp.$1.length==1)?(o[k]):(("00"+o[k]).substr((""+o[k]).length)));
23.returnfmt;
24.}
方法调用
vartime1=newDate().format("yyyy-MM-ddHH:mm:ss");
vartime2=newDate().format("yyyy-MM-dd");
方法二:
01.<mce:scriptlanguage="javascript"type="text/javascript"><!--
02.
03./**
04.*对Date的扩展,将Date转化为指定格式的String
05.*月(M)、日(d)、12小时(h)、24小时(H)、分(m)、秒(s)、周(E)、季度(q)可以用1-2个占位符
06.*年(y)可以用1-4个占位符,毫秒(S)只能用1个占位符(是1-3位的数字)
07.*eg:
08.*(newDate()).pattern("yyyy-MM-ddhh:mm:ss.S")==>2006-07-0208:09:04.423
09.*(newDate()).pattern("yyyy-MM-ddEHH:mm:ss")==>2009-03-10二20:09:04
10.*(newDate()).pattern("yyyy-MM-ddEEhh:mm:ss")==>2009-03-10周二08:09:04
11.*(newDate()).pattern("yyyy-MM-ddEEEhh:mm:ss")==>2009-03-10星期二08:09:04
12.*(newDate()).pattern("yyyy-M-dh:m:s.S")==>2006-7-28:9:4.18
13.*/
14.Date.prototype.pattern=function(fmt){
15.varo={
16."M+":this.getMonth()+1,//月份
17."d+":this.getDate(),//日
18."h+":this.getHours()%12==0?12:this.getHours()%12,//小时
19."H+":this.getHours(),//小时
20."m+":this.getMinutes(),//分
21."s+":this.getSeconds(),//秒
22."q+":Math.floor((this.getMonth()+3)/3),//季度
23."S":this.getMilliseconds()//毫秒
24.};
25.varweek={
26."0":"/u65e5",
27."1":"/u4e00",
28."2":"/u4e8c",
29."3":"/u4e09",
30."4":"/u56db",
31."5":"/u4e94",
32."6":"/u516d"
33.};
34.if(/(y+)/.test(fmt)){
35.fmt=fmt.replace(RegExp.$1,(this.getFullYear()+"").substr(4-RegExp.$1.length));
36.}
37.if(/(E+)/.test(fmt)){
38.fmt=fmt.replace(RegExp.$1,((RegExp.$1.length>1)?(RegExp.$1.length>2?"/u661f/u671f":"/u5468"):"")+week[this.getDay()+""]);
39.}
40.for(varkino){
41.if(newRegExp("("+k+")").test(fmt)){
42.fmt=fmt.replace(RegExp.$1,(RegExp.$1.length==1)?(o[k]):(("00"+o[k]).substr((""+o[k]).length)));
43.}
44.}
45.returnfmt;
46.}
47.
48.vardate=newDate();
49.window.alert(date.pattern("yyyy-MM-ddhh:mm:ss"));
50.//--></mce:script>
方法三:
01.Date.prototype.format=function(mask){
02.
03.vard=this;
04.
05.varzeroize=function(value,length){
06.
07.if(!length)length=2;
08.
09.value=String(value);
10.
11.for(vari=0,zeros='';i<(length-value.length);i++){
12.
13.zeros+='0';
14.
15.}
16.
17.returnzeros+value;
18.
19.};
20.
21.returnmask.replace(/"[^"]*"|'[^']*'|/b(?:d{1,4}|m{1,4}|yy(?:yy)?|([hHMstT])/1?|[lLZ])/b/g,function($0){
22.
23.switch($0){
24.
25.case'd':returnd.getDate();
26.
27.case'dd':returnzeroize(d.getDate());
28.
29.case'ddd':return['Sun','Mon','Tue','Wed','Thr','Fri','Sat'][d.getDay()];
30.
31.case'dddd':return['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'][d.getDay()];
32.
33.case'M':returnd.getMonth()+1;
34.
35.case'MM':returnzeroize(d.getMonth()+1);
36.
37.case'MMM':return['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'][d.getMonth()];
38.
39.case'MMMM':return['January','February','March','April','May','June','July','August','September','October','November','December'][d.getMonth()];
40.
41.case'yy':returnString(d.getFullYear()).substr(2);
42.
43.case'yyyy':returnd.getFullYear();
44.
45.case'h':returnd.getHours()%12||12;
46.
47.case'hh':returnzeroize(d.getHours()%12||12);
48.
49.case'H':returnd.getHours();
50.
51.case'HH':returnzeroize(d.getHours());
52.
53.case'm':returnd.getMinutes();
54.
55.case'mm':returnzeroize(d.getMinutes());
56.
57.case's':returnd.getSeconds();
58.
59.case'ss':returnzeroize(d.getSeconds());
60.
61.case'l':returnzeroize(d.getMilliseconds(),3);
62.
63.case'L':varm=d.getMilliseconds();
64.
65.if(m>99)m=Math.round(m/10);
66.
67.returnzeroize(m);
68.
69.case'tt':returnd.getHours()<12?'am':'pm';
70.
71.case'TT':returnd.getHours()<12?'AM':'PM';
72.
73.case'Z':returnd.toUTCString().match(/[A-Z]+$/);
74.
75.//Returnquotedstringswiththesurroundingquotesremoved
76.
77.default:return$0.substr(1,$0.length-2);
78.
79.}
80.
81.});
82.
83.};