Javasrcipt Date format Conversion

Source: Internet
Author: User

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;
}
Console.log (New Date (). Format ("YYYY-MM-DD"))
Console.log (New Date (). Format ("Yyyy-mm-dd hh:mm"))
Console.log (New Date (). Format ("Yyyy-mm-dd h:mm"))

/**
* Extension to date, converts date to a string of the specified format
* Months (m), days (d), 12 hours (h), 24 hours (h), minutes (m), seconds (s), Weeks (E), quarter (q) can be used with 1-2 placeholders
* Year (Y) can use 1-4 placeholders, milliseconds (S) only with 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": "Day",
"1": "One",
"2": "Two",
"3": "Three",
"4": "Four",
"5": "Five",
"6": "Six"
};
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?) "Week": "Week"): "") +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 ();
Console.log (Date.pattern ("Yyyy-mm-dd EEE hh:mm:ss"));

Javasrcipt Date format Conversion

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.