JS provides various property methods for acquiring time date objects, such as: GetDate method | Getday Method | getFullYear Method | GetHours method ... And so on, but did not like Java as a way to provide users with the template (pattern) provided by the user to format the specified time object, so they encapsulate a small method, only for everyone to ridicule-.-, there are good suggestions also look generous to recommend Oh.
Use the Knowledge point:
Arguments: This object represents the function being executed and the arguments of the function that called it. Cannot be explicitly created, although it has a length property and can be evaluated as an array in the "[]" syntax, but it is not an array. typeof object: An operator that returns a string representing the data type of an expression. Six possible: "Number," "String," "Boolean," "Object," "function," and "undefined." Object.constructor: Represents a function that creates an object. Object must be the name of an object or function. The underlying data does not have this attribute. Exec method: Runs a lookup in a string with a regular expression pattern and returns an array that contains the result of the lookup. No match found, it returns NULL. Matching elements of the same type in an array do not recur. Str.split (REX|STR): Splits a string into substrings with a regular object or substring, and then returns the result as an array of strings. Throw error (' msg '): Throws an error with message information. Throw can be followed by any expression. There are also some for. In use, three-mesh operator, substr these are not spoken, relatively simple.
Code fragment:
Copy Code code as follows:
/**
* ***JS Time Date Format * * * * <br>
* <p>
* Template string with rigorous format, the exception will throw exceptions, and each type of format can only occur once, such as: yyyy-mm-yyyy format will throw an exception
* </p>
* Y-year LENGTH:2/4 <br>
* Q-quarter length:1 <br>
* M-month length:1~2 <br>
* D-Day length:1~2 <br>
* H-time length:1~2 24-hour system, H:12-hour system <br>
* M-length:1~2 bit <br>
* S-second length:1~2 bit <br>
* S-Millisecond length: fixed 1-bit
* @param {Date type Object} date
* @param {String type template string} FMT
* @return formatted time date string
* @author Lyt
*/
function DateFormat (date, FMT) {
if (arguments.length!= 2)///parameter number check
Throw Error (' arguments length not valid ');
if (!date | | (typeof date!= ' object ') | | (D.constructor!= Date)) Validation of parameter validity
Throw Error (Arguments[0] + ': Type not date type ');
if (/h+/.test (FMT) &&/h+/.test (FMT))
Throw error ("Hour format errors, the same type can only appear once!") ");
/* template parameter check, regular verification method * *
var verify = function (Rex) {
var arr = new RegExp (REX). EXEC (FMT); Get an array of matching results
if (!arr)//Match failed to return
Return "";
if (fmt.split Rex. Length > 2)//multiple occurrences of the same type interval
Throw error ("FMT Malformed: Same type can only occur once!") ");
return arr[0];
};
/**
* Provide the month, day, time, minute, second universal matching replacement
* @param {object O property key} R
* @param {R corresponds to the regular object} Rex
**/
var common = function (r, Rex) {
if (Len!=1 && len!=2)
Throw error ("Bad month format: M can only appear 1/2 times");
Len = 2? Fmt=fmt.replace (Rex, o[r].length==1?) "0" +o[r]: O[r]): Fmt=fmt.replace (Rex, o[r));
}
var o = {//Data storage Object
"y+": date.getfullyear () + "",//year
"q+": Math.floor (Date.getmonth () + 3)/3),//Quarter
"m+": Date.getmonth () + 1 + "",//month
"D+": date.getdate () + "",//day
"h+": date.gethours () + "",//24 o'clock
"h+": date.gethours () + "",//12 o'clock
"m+": date.getminutes () + "",//min
"S+": date.getseconds () + "",//sec
"S+": date.getmilliseconds ()//MS
}
for (Var r in O) {
var rex, Len, temp;
Rex = new RegExp (r);
temp = Verify (Rex); Matching derived string
len = temp.length; Length
if (!len | | | len = = 0)
Continue
if (r = = "y+") {
if (Len!=2 && len!= 4)
Throw error ("Year format errors: Y can only appear 2/4 times");
Len = 2? Fmt=fmt.replace (Rex, O[r].substr (2,3)): Fmt=fmt.replace (Rex, o[r]);
else if (r = = "q+") {
if (Len!= 1)
Throw error ("Quarterly format error: Q can only appear 1 times");
Fmt=fmt.replace (Rex, o[r]);
else if (r = = "h+") {
if (Len!=1 && len!=2)
Throw error ("Hour format errors: H can only appear 1/2 times");
var h = (O[r] > O[r]-12:o[r]) + "";
Len = 2? Fmt=fmt.replace (Rex, h.length==1?) "0" +h:h): Fmt=fmt.replace (Rex, h);
else if (r = = "S+") {
if (Len!= 1)
Throw error ("Millisecond number is malformed: s can only appear 1 times");
Fmt=fmt.replace (Rex, o[r]);
}else {//(r== "m+" | | r== "d+" | | r== "h+" | | r== "m+" | | r== "S+")
Common (R, Rex)
}
}
return FMT;
}
Several demonstration effect, for everybody's Reference:
Copy Code code as follows:
Console.log (DateFormat (new Date), ' yyyy q quarter m DD Day hh (m minute s S milliseconds '));
Copy Code code as follows:
Console.log (DateFormat (new Date), ' yyyy yy Q quarter m DD Day hh (m minute s S milliseconds '));
Copy Code code as follows:
Console.log (DateFormat (new Date), ' yyyy q quarter m DD Day hh (m minute s S milliseconds '));
Copy Code code as follows:
Console.log ("I am not a time object", ' yyyy q quarter m month DD Day hh ' m minute s ' s milliseconds '));
Copy Code code as follows:
Console.log (DateFormat (new Date), ' yyyy q quarter mmm month dd Day hh ' m minute s S milliseconds ');
Other effects are not listed, interested in the code can be copied down directly to test the line, there are any bugs or need to optimize the place please be generous.
The above is this very useful packaging functions introduced, is not very practical, small partners can be used directly to their own projects.