📄 date.js
字号:
+ " v.add(Date.HOUR, (v.getGMTOffset() / 100) + (o / -100))) : v;\n" // reset to GMT, then add offset + "}"; Date.parseRegexes[regexNum] = new RegExp("^" + regex + "$", "i"); eval(code);};// privateDate.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: function() { return Ext.applyIf({ s:"(\\d{1,2})" // day of month without leading zeroes (1 - 31) }, Date.parseCodes["d"]); }, 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("|") + ")" }, Date.parseCodes["F"]); }, m: { g:1, c:"m = parseInt(results[{0}], 10) - 1;\n", s:"(\\d{2})" // month number with leading zeros (01 - 12) }, n: function() { return Ext.applyIf({ s:"(\\d{1,2})" // month number without leading zeros (1 - 12) }, Date.parseCodes["m"]); }, 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 Date.parseCodes["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 Date.parseCodes["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 Date.parseCodes["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 = parseInt(results[{0}], 10);\n", s:"(\\d{3})" // milliseconds with leading zeros (000 - 999) }, 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: function() { return Ext.applyIf({ s: "([+\-]\\d{2}:\\d{2})" // GMT offset in hrs and mins (with colon separator) }, Date.parseCodes["O"]); }, T: { g:0, c:null, s:"[A-Z]{1,4}" // timezone abbrev. may be between 1 - 4 chars }, Z: { g:1, c:"z = results[{0}] * 1;\n" // -43200 <= UTC offset <= 50400 + "z = (-43200 <= z && z <= 50400)? z : null;\n", s:"([+\-]?\\d{1,5})" // leading '+' sign is optional for UTC offset }, c: function() { var df = Date.formatCodeToRegex, calc = []; var arr = [ df("Y", 1), df("m", 2), df("d", 3), df("h", 4), df("i", 5), df("s", 6), {c:"if(results[7] == 'Z'){\no = 0;\n}else{\n" + df("P", 7).c + "\n}"} // allow both "Z" (i.e. UTC) and "+08:00" (i.e. GMT) time zone delimiters ]; for (var i = 0, l = arr.length; i < l; ++i) { calc.push(arr[i].c); } return { g:1, c:calc.join(""), s:arr[0].s + "-" + arr[1].s + "-" + arr[2].s + "T" + arr[3].s + ":" + arr[4].s + ":" + arr[5].s + "(" + df("P", 7).s + "|Z)" } }, U: { g:1, c:"u = parseInt(results[{0}], 10);\n", s:"(-?\\d+)" // leading minus sign indicates seconds before UNIX epoch }}// privateDate.formatCodeToRegex = function(character, currentGroup) { // Note: currentGroup - position in regex result array (see notes for Date.parseCodes above) var p = Date.parseCodes[character]; if (p) { p = Ext.type(p) == 'function'? p() : p; Date.parseCodes[character] = p; // reassign function result to prevent repeated execution if (p.c) { p.c = String.format(p.c, currentGroup); } } return p || { g:0, c:null, s:Ext.escapeRe(character) // treat unrecognised characters as literals }};/** * Get the timezone abbreviation of the current date (equivalent to the format specifier 'T'). * * Note: The date string returned by the javascript Date object's toString() method varies * between browsers (e.g. FF vs IE) and system region settings (e.g. IE in Asia vs IE in America). * For a given date string e.g. "Thu Oct 25 2007 22:55:35 GMT+0800 (Malay Peninsula Standard Time)", * getTimezone() first tries to get the timezone abbreviation from between a pair of parentheses * (which may or may not be present), failing which it proceeds to get the timezone abbreviation * from the GMT offset portion of the date string. * @return {String} The abbreviated timezone name (e.g. 'CST', 'PDT', 'EDT', 'MPST' ...). */Date.prototype.getTimezone = function() { // the following list shows the differences between date strings from different browsers on a WinXP SP2 machine from an Asian locale: // // Opera : "Thu, 25 Oct 2007 22:53:45 GMT+0800" -- shortest (weirdest) date string of the lot // Safari : "Thu Oct 25 2007 22:55:35 GMT+0800 (Malay Peninsula Standard Time)" -- value in parentheses always gives the correct timezone (same as FF) // FF : "Thu Oct 25 2007 22:55:35 GMT+0800 (Malay Peninsula Standard Time)" -- value in parentheses always gives the correct timezone // IE : "Thu Oct 25 22:54:35 UTC+0800 2007" -- (Asian system setting) look for 3-4 letter timezone abbrev // IE : "Thu Oct 25 17:06:37 PDT 2007" -- (American system setting) look for 3-4 letter timezone abbrev // // this crazy regex attempts to guess the correct timezone abbreviation despite these differences. // step 1: (?:\((.*)\) -- find timezone in parentheses // step 2: ([A-Z]{1,4})(?:[\-+][0-9]{4})?(?: -?\d+)?) -- if nothing was found in step 1, find timezone from timezone offset portion of date string // step 3: remove all non uppercase characters found in step 1 and 2 return this.toString().replace(/^.* (?:\((.*)\)|([A-Z]{1,4})(?:[\-+][0-9]{4})?(?: -?\d+)?)$/, "$1$2").replace(/[^A-Z]/g, "");};/** * Get the offset from GMT of the current date (equivalent to the format specifier 'O'). * @param {Boolean} colon true to separate the hours and minutes with a colon (defaults to false). * @return {String} The 4-character offset string prefixed with + or - (e.g. '-0600'). */Date.prototype.getGMTOffset = function(colon) { return (this.getTimezoneOffset() > 0 ? "-" : "+") + String.leftPad(Math.abs(Math.floor(this.getTimezoneOffset() / 60)), 2, "0") + (colon ? ":" : "") + String.leftPad(this.getTimezoneOffset() % 60, 2, "0");};/** * Get the numeric day number of the year, adjusted for leap year. * @return {Number} 0 to 364 (365 in leap years). */Date.prototype.getDayOfYear = function() { var num = 0; Date.daysInMonth[1] = this.isLeapYear() ? 29 : 28; for (var i = 0; i < this.getMonth(); ++i) { num += Date.daysInMonth[i]; } return num + this.getDate() - 1;};/** * Get the numeric ISO-8601 week number of the year. * (equivalent to the format specifier 'W', but without a leading zero). * @return {Number} 1 to 53 */Date.prototype.getWeekOfYear = function() { // adapted from http://www.merlyn.demon.co.uk/weekcalc.htm var ms1d = 864e5; // milliseconds in a day var ms7d = 7 * ms1d; // milliseconds in a week var DC3 = Date.UTC(this.getFullYear(), this.getMonth(), this.getDate() + 3) / ms1d; // an Absolute Day Number var AWN = Math.floor(DC3 / 7); // an Absolute Week Number var Wyr = new Date(AWN * ms7d).getUTCFullYear(); return AWN - Math.floor(Date.UTC(Wyr, 0, 7) / ms7d) + 1;};/** * Whether or not the current date is in a leap year. * @return {Boolean} True if the current date is in a leap year, else false. */Date.prototype.isLeapYear = function() { var year = this.getFullYear(); return !!((year & 3) == 0 && (year % 100 || (year % 400 == 0 && year)));};/** * Get the first day of the current month, adjusted for leap year. The returned value * is the numeric day index within the week (0-6) which can be used in conjunction with * the {@link #monthNames} array to retrieve the textual day name. * Example: *<pre><code>var dt = new Date('1/10/2007');document.write(Date.dayNames[dt.getFirstDayOfMonth()]); //output: 'Monday'</code></pre> * @return {Number} The day number (0-6). */Date.prototype.getFirstDayOfMonth = function() { var day = (this.getDay() - (this.getDate() - 1)) % 7; return (day < 0) ? (day + 7) : day;};/**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -