日期格式化方法摘要

方法1:


将日期、日期扩展为指定的格式字符串。
月(m)和日本(d)、小时(h)、(m)、第二季度(s)、(q)1-2可用作占位符,
/ /年(Y)可用于1-4占位符,MS(S)只能使用1个占位符(1-3位)
例:
(新)(日期)/。格式(yyyy-mm-dd HH:毫米:SS的2006-07-02 08:09: 4.423 = = >)
(新)(日期)/。格式(yyyy-m-d H:M:代表2006-7-2 8:9: 4.18 = = >)
date.prototype.format =功能(FMT){ / /作者:meizz
var { { { {
M +:this.getmonth(+ 1) / /月
D:(这个getdate), / /天
H:(这个,获得当前的小时) / /小时。
M + :(这个,获得当前的分钟), / /分
+:(这个,获得当前的秒), / /
Q+:Math.floor((,获得当前的月份)(+ 3)/ 3), / /季度
S:(这个。getmilliseconds) / /小姐
};
如果( / /(Y +)。测试(FMT))= fmt.replace FMT(regexp。1美元,(this.getfullyear()+)。Substr(4—9。1美元。长度));
对于(k在O中k)
如果(new RegExp((钾))。测试(FMT)fmt.replace(FMT)= regexp。1美元(1美元= = 1.length regexp。)(O { }(K):(00 + K O { })(Substr(。。长度+ O K)))){ };
返回格式化;
}




调用模式:

VAR时间=新的日期()。格式(yyyy-mm-dd);

VAR时间=新的日期()。格式(yyyy-mm-dd HH:毫米:SS );

方法二:





<!-日期扩展,将转换为日期格式指定的字符串*(m)、日本(d)、12小时(h)、24小时(h)、(m)、(s)、第二周(e)、季度(q)。
你可以使用占位符* 1-2年(Y)使用1-4占位符,并只能使用1个占位符(1-3位的数量)*如:*(新
(日期))。模式(yyyy-mm-dd HH:毫米:SS的2006-07-02 08:09: 4.423 = = >)
*(新)(模式)(yyyy-mm-dd E HH:毫米:SS)= = > 2009-03-10两20:09:04
*(新)(模式)(yyyy-mm-dd EE HH:毫米:SS)= = > 2009-03-10星期二08:09:04
*(新)(模式)(yyyy-mm-dd EEE HH:毫米:SS)= = > 2009-03-10星期二08:09:04
*(新)(模式)(yyyy-m-d H:M:代表2006-7-2 8:9: 4.18 = = >)
* /
日期。原型。模式=功能(FMT){
var { { { {
M +:this.getmonth(+ 1), / /月
D:(这个getdate), / /天
H:this.gethours(占12):this.gethours(% 12 = = 012) / /小时。
H:(这个,获得当前的小时) / /小时。
M + :(这个,获得当前的分钟), / /分
+:(这个,获得当前的秒), / /
Q+:Math.floor((,获得当前的月份)+ 3(或3)) / /季度。
S:(这个。getmilliseconds) / /小姐
};
var周= {
0 / u65e5
1 / u4e00
2 / u4e8c
3 / u4e09
4 / u56db
5 / u4e94
6 / u516d
};
如果( / /(Y +)。测试(FMT)){
FMT = fmt.replace(regexp。1美元,((this.getfullyear()+)。Substr(4—9。1美元。长度));
}
如果( / /(E +)。测试(FMT)){
FMT = fmt.replace(regexp。1美元,((regexp。1美元。长度>1)(regexp。1美元。长> 2 / / u661f u671f / u5468 ):)+周{ this.getday()+}));
}
对于(var在o中){
如果(新规则((钾))。测试(FMT)){
FMT = fmt.replace(regexp。1美元,(regexp。1美元。长度= = 1)(O { k }):(00 + O { k })。Substr((+ O { k })。长度))));
}
}
返回格式化;
}

日期=新日期();
Window.alert(date.pattern(yyyy-mm-dd HH:毫米:SS));





方法三:


date.prototype.format =功能(面具){
var =;
无功补零=函数(值长){
如果(!长度)长度= 2;
值=字符串(值);
对于(var i = 0,零=);i <(长度-值-长度);i + +){
零点= '0';
}
返回0 +值;
};
返回(mask.replace / }×(|{ }^| / B{ ^{:D
1, 4
| {米}
1, 4
| } YY(YY)|({ hhmstt }) / 1 | { LLZ } / B / G),功能(0美元){
开关(0美元){
案件':
返回d.getdate();
case'dd:
回补零(d.getdate());
case'ddd:
返回{'太阳','mon ','tue ','wed ','thr ','fri ','sat} { d.getday()};
case'dddd:
返回{ 'sunday ','monday ','tuesday ','wednesday ','星期四',周五,'saturday} { d.getday()};
情况是:
返回d.getmonth()+ 1;
case'mm:
回补零(d.getmonth()+ 1);
case'mmm:
返回{ 'jan ','feb ','mar ','apr ',愿,'jun ','jul ','aug ','sep ','华侨城','nov ','nov '()};
case'mmmm:
返回{ 'january ','february ','march '四',愿,6月,'july ','august '、'九月','october ','november ','november '()};
case'yy:
返回的字符串(d.getfullyear()),Substr(2);
case'yyyy:
返回d.getfullyear();
case'h:
返回(d.gethours)| | % 12 12;
case'hh:
回补零(d.gethours)12 | | %(12);
case'h:
返回d.gethours();
case'hh:
回补零(d.gethours());
情况是:
返回d.getminutes();
case'mm:
回补零(d.getminutes());
案例':
返回d.getseconds();
case'ss:
回补零(d.getseconds());
case'l:
回补零(d.getmilliseconds(),3);
case'l:
var m = d.getmilliseconds();
如果(m>99)M = math.round(M / 10);
回补零(M);
case'tt:
返回d.gethours()< 12'am:'pm;
case'tt:
返回d.gethours()< 12'am:'pm;
case'z:
返回d.toutcstring()。比赛( / { A-Z } + $ /);
返回引用的字符串,去掉周围的引号
违约:
返回0.substr美元(1美元0.length - 2);
}
});
};




方法四:



日期=新日期();
document.writeln(日期);
/ / Thu Jan 082015 01:25:53 GMT + 0800(中国标准时间)
document.writeln(Date.parse());

document.writeln(Date.parse(6 / 10 / 2014));
1402329600000毫秒
document.writeln(Date.parse('thu Jan 082015 01:25:53 '));
1420651553000毫秒
document.writeln(Date.UTC());

日期格式化方法
document.writeln(' ');
document.writeln(date.todatestring());
/ / Thu Jan 082015
document.writeln(' ')
document.writeln(date.totimestring());
/ / 01:39:08 GMT + 0800(中国标准时间)
document.writeln(' ')
document.writeln(date.tolocaledatestring());
1月8日2015
document.writeln(' ')
document.writeln(date.tolocaletimestring());
早上/ 1:39:08
document.writeln(' ')
document.writeln(date.toutcstring());
/ /结婚,07月2015日17:39:08 GMT
一些集合get方法。



方法五:


date.prototype.format =功能(formatstr){
var str = formatstr;
var一周=日,A,二,三,四,五,六};
STR = str.replace( /年| yyyy /,this.getfullyear());
STR = str.replace( / / | YY YY,((this.getyear()% 100)> 9(this.getyear()% 100)。ToString():0+(this.getyear()% 100)));
STR = str.replace( / /毫米,((this.getmonth()+ 1)> 9(this.getmonth)+(1)。ToString():0+(this.getmonth()+ 1)));
STR = str.replace( / / g,((this.getmonth)+(1));
STR = str.replace( / W | W / g,周{ this.getday())))
STR = str.replace( / / DD | DD,this.getdate()> 9 this.getdate()ToString()):0+ this.getdate()));
STR = str.replace( / D | D / G,this.getdate());
STR = str.replace( / / | HH HH,this.gethours()> 9 this.gethours()ToString()):0+ this.gethours()));
STR = str.replace( / H | H / g,this.gethours());
STR = str.replace( / /毫米,this.getminutes()> 9 this.getminutes()ToString()):0+ this.getminutes()));
STR = str.replace( / / g,this.getminutes());
STR = str.replace( / / | SS SS,this.getseconds()> 9 this.getseconds()ToString()):0+ this.getseconds()));
STR = str.replace( / S | S/g,this.getseconds());
返回字符
}


方法六:


date.prototype.format =功能(formatstr){
var str = formatstr;
var一周=日,A,二,三,四,五,六};
STR = str.replace( /年| yyyy /,this.getfullyear());
STR = str.replace( / / | YY YY,((this.getyear()% 100)> 9(this.getyear()% 100)。ToString():0+(this.getyear()% 100)));
STR = str.replace( / /毫米,((this.getmonth()+ 1)> 9(this.getmonth)+(1)。ToString():0+(this.getmonth()+ 1)));
STR = str.replace( / / g,((this.getmonth)+(1));
STR = str.replace( / W | W / g,周{ this.getday())))
STR = str.replace( / / DD | DD,this.getdate()> 9 this.getdate()ToString()):0+ this.getdate()));
STR = str.replace( / D | D / G,this.getdate());
STR = str.replace( / / | HH HH,this.gethours()> 9 this.gethours()ToString()):0+ this.gethours()));
STR = str.replace( / H | H / g,this.gethours());
STR = str.replace( / /毫米,this.getminutes()> 9 this.getminutes()ToString()):0+ this.getminutes()));
STR = str.replace( / / g,this.getminutes());
STR = str.replace( / / | SS SS,this.getseconds()> 9 this.getseconds()ToString()):0+ this.getseconds()));
STR = str.replace( / S | S/g,this.getseconds());
返回字符
}


方法七:


*
1,60年代,刚刚展示
2, 1min < 60 > = min, and the display time difference XX minutes ago
3、60min <> = 1、和显示时间差:XX XX的今天
4、1day <> = 1年、六月XX XX XX XX显示日期:
5、1year > =,显示特定日期XXXX年XX月XX日XX:XX
* /
作用时间格式(时间){
日期=新日期(时间),
CURDATE =新的日期(),
年为date.getfullyear(),
月date.getmonth()+ 10 =,
天date.getdate(),
小时= date.gethours(),
分钟= date.getminutes(),
curyear = curdate.getfullyear(),
CurHour = curDate.getHours (),
timestr;

如果(年<< curyear){
timestr =年+月++年月日+日+小时:分钟;
其他{ }
VaR的消遣= CURDATE日期,
路径=消遣 / 3600000;

如果(路径> curhour){
timestr =月+日+月+小时:分钟;
} else if(路径> = 1){
timestr = +时间++今天:一分钟;
其他{ }
无功pastm = curdate.getminutes()分钟;
如果(pastm > 1){
timestr = pastm +分;
其他{ }
timestr = '只是';
}
}
}
返回timestr;
}


是不是第七个人化了