📄 date.js
字号:
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++; Date.formatFunctions[format] = funcName; var code = "Date.prototype." + funcName + " = function(){return "; var special = false; var ch = ''; 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(format) { var funcName = "parse" + Date.parseFunctions.count++; var regexNum = Date.parseRegexes.length; var currentGroup = 1; Date.parseFunctions[format] = funcName; var code = "Date." + funcName + " = function(input){\n" + "var y, m, d, h = 0, i = 0, s = 0, ms = 0, o, z, u, v;\n" + "input = String(input);\n" + "d = new Date();\n" + "y = d.getFullYear();\n" + "m = d.getMonth();\n" + "d = d.getDate();\n" + "var results = input.match(Date.parseRegexes[" + regexNum + "]);\n" + "if (results && results.length > 0) {"; var regex = ""; var special = false; var ch = ''; 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 = Date.formatCodeToRegex(ch, currentGroup); currentGroup += obj.g; regex += obj.s; if (obj.g && obj.c) { code += obj.c; } } } code += "if (u){\n" + "v = new Date(u * 1000);\n" // give top priority to UNIX time + "}else if (y >= 0 && m >= 0 && d > 0 && h >= 0 && i >= 0 && s >= 0 && ms >= 0){\n" + "v = new Date(y, m, d, h, i, s, ms);\n" + "}else if (y >= 0 && m >= 0 && d > 0 && h >= 0 && i >= 0 && s >= 0){\n" + "v = new Date(y, m, d, h, i, s);\n" + "}else if (y >= 0 && m >= 0 && d > 0 && h >= 0 && i >= 0){\n" + "v = new Date(y, m, d, h, i);\n" + "}else if (y >= 0 && m >= 0 && d > 0 && h >= 0){\n" + "v = new Date(y, m, d, h);\n" + "}else if (y >= 0 && m >= 0 && d > 0){\n" + "v = new Date(y, m, d);\n" + "}else if (y >= 0 && m >= 0){\n" + "v = new Date(y, m);\n" + "}else if (y >= 0){\n" + "v = new Date(y);\n" + "}\n}\nreturn (v && (z || o))?" // 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;\n" // reset to GMT, then add offset + "}"; Date.parseRegexes[regexNum] = new RegExp("^" + regex + "$", "i"); eval(code); }, // 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+)" // milliseconds with leading zeroes (arbitrary number of digits allowed) e.g. 001, 100, 999, 999876543210 }, 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 + -