JavaScript date format (JS dates formatted)

Source: Internet
Author: User

Method One:

An extension to date that converts date to a string of the specified format
The month (m), Day (d), hour (h), Minute (m), second (s), quarter (q) can be used with 1-2 placeholders,
Year (y) can use 1-4 placeholders, milliseconds (S) with only 1 placeholders (1-3 digits)
Example:
(New Date ()). Format ("Yyyy-mm-dd hh:mm:ss. S ") ==> 2006-07-02 08:09:04.423
(New Date ()). Format ("yyyy-m-d h:m:s.s") ==> 2006-7-2 8:9:4.18
Date.prototype.Format = function (FMT) {//author:meizz
var o = {
"m+": This.getmonth () + 1,//month
"D+": this.getdate (),//day
"H +": this.gethours (),//hour
"m+": this.getminutes (),//min
"S+": This.getseconds (),//sec
"q+": Math.floor ((This.getmonth () + 3)/3),//Quarterly
"S": this.getmilliseconds ()//MS
};
if (/(y+)/.test (FMT)) FMT = Fmt.replace (regexp.$1, (this.getfullyear () + ""). substr (4-regexp.$1.length));
For (var k in O)
if (New RegExp ("(" + K + ")"). Test (FMT)) FMT = Fmt.replace (regexp.$1, (regexp.$1.length = = 1)? (O[k]): (("XX" + o[k]). substr (("" + O[k]).));
return FMT;
}

Call:

var time1 = new Date (). Format ("Yyyy-mm-dd"); var time2 = new Date (). Format ("Yyyy-mm-dd HH:mm:ss");

Method Two:

<script language= "javascript" type= "Text/javascript" >
<!--/** * Extension to date, converts date to a specified format of string * months (M), Day (d), 12 hours (h), 24 hours (h), minutes (m), seconds (s), Weeks (E), quarter (Q)
You can use 1-2 placeholders * year (y) to use 1-4 placeholders, milliseconds (S) can only use 1 placeholders (1-3 digits) * Eg: * (new
Date ()). Pattern ("Yyyy-mm-dd hh:mm:ss. S ") ==> 2006-07-02 08:09:04.423
* (New Date ()). Pattern ("Yyyy-mm-dd E HH:mm:ss") ==> 2009-03-10 II 20:09:04
* (New Date ()). Pattern ("Yyyy-mm-dd EE hh:mm:ss") ==> 2009-03-10 Tuesday 08:09:04
* (New Date ()). Pattern ("Yyyy-mm-dd EEE hh:mm:ss") ==> 2009-03-10 Tuesday 08:09:04
* (New Date ()). Pattern ("yyyy-m-d h:m:s.s") ==> 2006-7-2 8:9:4.18
*/
Date.prototype.pattern=function (FMT) {
var o = {
"m+": This.getmonth () +1,//month
"D+": this.getdate (),//day
"H +": this.gethours ()%12 = = 0? 12:this.gethours ()%12,//hour
"H +": this.gethours (),//hour
"m+": this.getminutes (),//min
"S+": This.getseconds (),//sec
"q+": Math.floor ((This.getmonth () +3)/3),//Quarterly
"S": this.getmilliseconds ()//MS
};
var week = {
"0": "/u65e5",
"1": "/u4e00",
"2": "/u4e8c",
"3": "/u4e09",
"4": "/u56db",
"5": "/u4e94",
"6": "/U516D"
};
if (/(y+)/.test (FMT)) {
Fmt=fmt.replace (Regexp.$1, (this.getfullyear () + ""). substr (4-regexp.$1.length));
}
if (/(e+)/.test (FMT)) {
Fmt=fmt.replace (regexp.$1, (regexp.$1.length>1)? (regexp.$1.length>2?) "/u661f/u671f": "/u5468"): "") +week[this.getday () + ""]);
}
For (var k in O) {
if (New RegExp ("(" + K + ")"). Test (FMT)) {
FMT = Fmt.replace (regexp.$1, (regexp.$1.length==1)? (O[k]): (("XX" + o[k]). substr (("" + O[k]).));
}
}
return FMT;
}

var date = new Date ();
Window.alert (Date.pattern ("Yyyy-mm-dd hh:mm:ss"));
-
</script>

Method Three:

Date.prototype.format = function (mask) {
var d = this;

var zeroize = function (value, length) {

if (!length) length = 2;

Value = String (value);

for (var i = 0, zeros = "; I < (length-value.length); i++) {

Zeros + = ' 0 ';

}

return zeros + value;

};

return Mask.replace (/"[^"]* "|" [^ ']* ' |/b (?: D {
1, 4
} | m {
1, 4
} | YY (?: yy)? | ([HHMSTT])/1? | [LLZ]) /b/g, function ($) {

Switch ($) {

Case ' d ':
return D.getdate ();

Case ' DD ':
Return Zeroize (D.getdate ());

Case ' DDD ':
return [' Sun ', ' Mon ', ' Tue ', ' Wed ', ' Thr ', ' Fri ', ' Sat '][d.getday ()];

Case ' dddd ':
return [' Sunday ', ' Monday ', ' Tuesday ', ' Wednesday ', ' Thursday ', ' Friday ', ' Saturday '][d.getday ()];

Case ' M ':
return D.getmonth () + 1;

Case ' MM ':
Return Zeroize (D.getmonth () + 1);

Case ' MMM ':
return [' Jan ', ' Feb ', ' Mar ', ' Apr ', ' may ', ' June ', ' Jul ', '][d.getmonth ', ' Sep ', ' Oct ', ' Nov ', ' Dec ' ()];

Case ' MMMM ':
return [' January ', ' February ', ' March ', ' April ', ' may ', ' June ', ' July ', ' August ', ' September ', ' October ', ' November ', ' Dec Ember '][d.getmonth ()];

Case ' yy ':
Return String (D.getfullyear ()). substr (2);

Case ' yyyy ':
return D.getfullyear ();

Case ' H ':
Return d.gethours ()% 12 | | 12;

Case ' hh ':
Return Zeroize (d.gethours ()% 12 | | 12);

Case ' H ':
return d.gethours ();

Case ' HH ':
Return Zeroize (D.gethours ());

Case ' m ':
return D.getminutes ();

Case ' mm ':
Return Zeroize (D.getminutes ());

Case ' s ':
return D.getseconds ();

Case ' SS ':
Return Zeroize (D.getseconds ());

Case ' l ':
Return Zeroize (D.getmilliseconds (), 3);

Case ' L ':
var m = d.getmilliseconds ();

if (M > a) m = Math.Round (M/10);

Return Zeroize (m);

Case ' TT ':
Return D.gethours () < 12? ' Am ': ' PM ';

Case ' TT ':
Return D.gethours () < 12? ' AM ': ' PM ';

Case ' Z ':
Return d.toutcstring (). Match (/[a-z]+$/);

Return quoted strings with the surrounding quotes removed

Default
Return $0.substr (1, $0.length-2);

}

});

}; transfer from http://www.cnblogs.com/zhangpengshou/archive/2012/07/19/2599053.html

JavaScript date format (JS dates formatted)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.