📄 date.js
字号:
H: "String.leftPad(this.getHours(), 2, '0')",
i: "String.leftPad(this.getMinutes(), 2, '0')",
s: "String.leftPad(this.getSeconds(), 2, '0')",
u: "String.leftPad(this.getMilliseconds(), 3, '0')",
O: "this.getGMTOffset()",
P: "this.getGMTOffset(true)",
T: "this.getTimezone()",
Z: "(this.getTimezoneOffset() * -60)",
c: function() { // ISO-8601 -- GMT format
for (var c = "Y-m-dTH:i:sP", code = [], i = 0, l = c.length; i < l; ++i) {
var e = c.charAt(i);
code.push(e == "T" ? "'T'" : Date.getFormatCode(e)); // treat T as a character literal
}
return code.join(" + ");
},
/*
c: function() { // ISO-8601 -- UTC format
return [
"this.getUTCFullYear()", "'-'",
"String.leftPad(this.getUTCMonth() + 1, 2, '0')", "'-'",
"String.leftPad(this.getUTCDate(), 2, '0')",
"'T'",
"String.leftPad(this.getUTCHours(), 2, '0')", "':'",
"String.leftPad(this.getUTCMinutes(), 2, '0')", "':'",
"String.leftPad(this.getUTCSeconds(), 2, '0')",
"'Z'"
].join(" + ");
},
*/
U: "Math.round(this.getTime() / 1000)"
},
/**
* Parses the passed string using the specified format. Note that this function expects dates in normal calendar
* format, meaning that months are 1-based (1 = January) and not zero-based like in JavaScript dates. Any part of
* the date format that is not specified will default to the current date value for that part. Time parts can also
* be specified, but default to 0. Keep in mind that the input date string must precisely match the specified format
* string or the parse operation will fail.
* Example Usage:
*<pre><code>
//dt = Fri May 25 2007 (current date)
var dt = new Date();
//dt = Thu May 25 2006 (today's month/day in 2006)
dt = Date.parseDate("2006", "Y");
//dt = Sun Jan 15 2006 (all date parts specified)
dt = Date.parseDate("2006-01-15", "Y-m-d");
//dt = Sun Jan 15 2006 15:20:01 GMT-0600 (CST)
dt = Date.parseDate("2006-01-15 3:20:01 PM", "Y-m-d h:i:s A" );
</code></pre>
* @param {String} input The unparsed date as a string.
* @param {String} format The format the date is in.
* @return {Date} The parsed date.
* @static
*/
parseDate : function(input, format) {
var p = Date.parseFunctions;
if (p[format] == null) {
Date.createParser(format);
}
var func = p[format];
return Date[func](input);
},
// private
getFormatCode : function(character) {
var f = Date.formatCodes[character];
if (f) {
f = Ext.type(f) == 'function'? f() : f;
Date.formatCodes[character] = f; // reassign function result to prevent repeated execution
}
// note: unknown characters are treated as literals
return f || ("'" + String.escape(character) + "'");
},
// private
createNewFormat : function(format) {
var funcName = "format" + Date.formatFunctions.count++,
code = "Date.prototype." + funcName + " = function(){return ",
special = false,
ch = '';
Date.formatFunctions[format] = funcName;
for (var i = 0; i < format.length; ++i) {
ch = format.charAt(i);
if (!special && ch == "\\") {
special = true;
}
else if (special) {
special = false;
code += "'" + String.escape(ch) + "' + ";
}
else {
code += Date.getFormatCode(ch) + " + ";
}
}
eval(code.substring(0, code.length - 3) + ";}");
},
// private
createParser : function() {
var code = [
"Date.{0} = function(input){",
"var y, m, d, h = 0, i = 0, s = 0, ms = 0, o, z, u, v;",
"input = String(input);",
"d = new Date();",
"y = d.getFullYear();",
"m = d.getMonth();",
"d = d.getDate();",
"var results = input.match(Date.parseRegexes[{1}]);",
"if(results && results.length > 0){",
"{2}",
"if(u){",
"v = new Date(u * 1000);", // give top priority to UNIX time
"}else if (y >= 0 && m >= 0 && d > 0 && h >= 0 && i >= 0 && s >= 0 && ms >= 0){",
"v = new Date(y, m, d, h, i, s, ms);",
"}else if (y >= 0 && m >= 0 && d > 0 && h >= 0 && i >= 0 && s >= 0){",
"v = new Date(y, m, d, h, i, s);",
"}else if (y >= 0 && m >= 0 && d > 0 && h >= 0 && i >= 0){",
"v = new Date(y, m, d, h, i);",
"}else if (y >= 0 && m >= 0 && d > 0 && h >= 0){",
"v = new Date(y, m, d, h);",
"}else if (y >= 0 && m >= 0 && d > 0){",
"v = new Date(y, m, d);",
"}else if (y >= 0 && m >= 0){",
"v = new Date(y, m);",
"}else if (y >= 0){",
"v = new Date(y);",
"}",
"}",
"return (v && (z != null || o != null))?" // favour UTC offset over GMT offset
+ " (Ext.type(z) == 'number' ? v.add(Date.SECOND, -v.getTimezoneOffset() * 60 - z) :" // reset to UTC, then add offset
+ " v.add(Date.MINUTE, -v.getTimezoneOffset() + (sn == '+'? -1 : 1) * (hr * 60 + mn))) : v;", // reset to GMT, then add offset
"}"
].join('\n');
return function(format) {
var funcName = "parse" + Date.parseFunctions.count++,
regexNum = Date.parseRegexes.length,
currentGroup = 1,
calc = "",
regex = "",
special = false,
ch = "";
Date.parseFunctions[format] = funcName;
for (var i = 0; i < format.length; ++i) {
ch = format.charAt(i);
if (!special && ch == "\\") {
special = true;
}
else if (special) {
special = false;
regex += String.escape(ch);
}
else {
var obj = $f(ch, currentGroup);
currentGroup += obj.g;
regex += obj.s;
if (obj.g && obj.c) {
calc += obj.c;
}
}
}
Date.parseRegexes[regexNum] = new RegExp("^" + regex + "$", "i");
eval(xf(code, funcName, regexNum, calc));
}
}(),
// private
parseCodes : {
/*
* Notes:
* g = {Number} calculation group (0 or 1. only group 1 contributes to date calculations.)
* c = {String} calculation method (required for group 1. null for group 0. {0} = currentGroup - position in regex result array)
* s = {String} regex pattern. all matches are stored in results[], and are accessible by the calculation mapped to 'c'
*/
d: {
g:1,
c:"d = parseInt(results[{0}], 10);\n",
s:"(\\d{2})" // day of month with leading zeroes (01 - 31)
},
j: {
g:1,
c:"d = parseInt(results[{0}], 10);\n",
s:"(\\d{1,2})" // day of month without leading zeroes (1 - 31)
},
D: function() {
for (var a = [], i = 0; i < 7; a.push(Date.getShortDayName(i)), ++i); // get localised short day names
return {
g:0,
c:null,
s:"(?:" + a.join("|") +")"
}
},
l: function() {
return {
g:0,
c:null,
s:"(?:" + Date.dayNames.join("|") + ")"
}
},
N: {
g:0,
c:null,
s:"[1-7]" // ISO-8601 day number (1 (monday) - 7 (sunday))
},
S: {
g:0,
c:null,
s:"(?:st|nd|rd|th)"
},
w: {
g:0,
c:null,
s:"[0-6]" // javascript day number (0 (sunday) - 6 (saturday))
},
z: {
g:0,
c:null,
s:"(?:\\d{1,3})" // day of the year (0 - 364 (365 in leap years))
},
W: {
g:0,
c:null,
s:"(?:\\d{2})" // ISO-8601 week number (with leading zero)
},
F: function() {
return {
g:1,
c:"m = parseInt(Date.getMonthNumber(results[{0}]), 10);\n", // get localised month number
s:"(" + Date.monthNames.join("|") + ")"
}
},
M: function() {
for (var a = [], i = 0; i < 12; a.push(Date.getShortMonthName(i)), ++i); // get localised short month names
return Ext.applyIf({
s:"(" + a.join("|") + ")"
}, $f("F"));
},
m: {
g:1,
c:"m = parseInt(results[{0}], 10) - 1;\n",
s:"(\\d{2})" // month number with leading zeros (01 - 12)
},
n: {
g:1,
c:"m = parseInt(results[{0}], 10) - 1;\n",
s:"(\\d{1,2})" // month number without leading zeros (1 - 12)
},
t: {
g:0,
c:null,
s:"(?:\\d{2})" // no. of days in the month (28 - 31)
},
L: {
g:0,
c:null,
s:"(?:1|0)"
},
o: function() {
return $f("Y");
},
Y: {
g:1,
c:"y = parseInt(results[{0}], 10);\n",
s:"(\\d{4})" // 4-digit year
},
y: {
g:1,
c:"var ty = parseInt(results[{0}], 10);\n"
+ "y = ty > Date.y2kYear ? 1900 + ty : 2000 + ty;\n", // 2-digit year
s:"(\\d{1,2})"
},
a: {
g:1,
c:"if (results[{0}] == 'am') {\n"
+ "if (h == 12) { h = 0; }\n"
+ "} else { if (h < 12) { h += 12; }}",
s:"(am|pm)"
},
A: {
g:1,
c:"if (results[{0}] == 'AM') {\n"
+ "if (h == 12) { h = 0; }\n"
+ "} else { if (h < 12) { h += 12; }}",
s:"(AM|PM)"
},
g: function() {
return $f("G");
},
G: {
g:1,
c:"h = parseInt(results[{0}], 10);\n",
s:"(\\d{1,2})" // 24-hr format of an hour without leading zeroes (0 - 23)
},
h: function() {
return $f("H");
},
H: {
g:1,
c:"h = parseInt(results[{0}], 10);\n",
s:"(\\d{2})" // 24-hr format of an hour with leading zeroes (00 - 23)
},
i: {
g:1,
c:"i = parseInt(results[{0}], 10);\n",
s:"(\\d{2})" // minutes with leading zeros (00 - 59)
},
s: {
g:1,
c:"s = parseInt(results[{0}], 10);\n",
s:"(\\d{2})" // seconds with leading zeros (00 - 59)
},
u: {
g:1,
c:"ms = results[{0}]; ms = parseInt(ms, 10)/Math.pow(10, ms.length - 3);\n",
s:"(\\d+)" // decimal fraction of a second (minimum = 1 digit, maximum = unlimited)
},
O: {
g:1,
c:[
"o = results[{0}];",
"var sn = o.substring(0,1);", // get + / - sign
"var hr = o.substring(1,3)*1 + Math.floor(o.substring(3,5) / 60);", // get hours (performs minutes-to-hour conversion also, just in case)
"var mn = o.substring(3,5) % 60;", // get minutes
"o = ((-12 <= (hr*60 + mn)/60) && ((hr*60 + mn)/60 <= 14))? (sn + String.leftPad(hr, 2, '0') + String.leftPad(mn, 2, '0')) : null;\n" // -12hrs <= GMT offset <= 14hrs
].join("\n"),
s: "([+\-]\\d{4})" // GMT offset in hrs and mins
},
P: {
g:1,
c:[
"o = results[{0}];",
"var sn = o.substring(0,1);", // get + / - sign
"var hr = o.substring(1,3)*1 + Math.floor(o.substring(4,6) / 60);", // get hours (performs minutes-to-hour conversion also, just in case)
"var mn = o.substring(4,6) % 60;", // get minutes
"o = ((-12 <= (hr*60 + mn)/60) && ((hr*60 + mn)/60 <= 14))? (sn + String.leftPad(hr, 2, '0') + String.leftPad(mn, 2, '0')) : null;\n" // -12hrs <= GMT offset <= 14hrs
].join("\n"),
s: "([+\-]\\d{2}:\\d{2})" // GMT offset in hrs and mins (with colon separator)
},
T: {
g:0,
c:null,
s:"[A-Z]{1,4}" // timezone abbrev. may be between 1 - 4 chars
},
Z: {
g:1,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -