📄 date.js
字号:
code += obj.c; } } } code += "if (u)\n" + "{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)\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" + "}return (v && (z || o))?\n" // favour UTC offset over GMT offset + " (z ? v.add(Date.SECOND, (v.getTimezoneOffset() * 60) + (z*1)) :\n" // reset to UTC, then add offset + " 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.formatCodeToRegex = function(character, currentGroup) { /* * currentGroup = position in regex result array * g = calculation group (0 or 1. only group 1 contributes to date calculations.) * c = calculation method (required for group 1. null for group 0.) * s = regex string */ switch (character) { case "d": return {g:1, c:"d = parseInt(results[" + currentGroup + "], 10);\n", s:"(\\d{2})"}; // day of month with leading zeroes (01 - 31) case "D": for (var a = [], i = 0; i < 7; a.push(Date.getShortDayName(i)), ++i); // get L10n short day names return {g:0, c:null, s:"(?:" + a.join("|") +")"}; case "j": return {g:1, c:"d = parseInt(results[" + currentGroup + "], 10);\n", s:"(\\d{1,2})"}; // day of month without leading zeroes (1 - 31) case "l": return {g:0, c:null, s:"(?:" + Date.dayNames.join("|") + ")"}; case "N": return {g:0, c:null, s:"[1-7]"}; // ISO-8601 day number (1 (monday) - 7 (sunday)) case "S": return {g:0, c:null, s:"(?:st|nd|rd|th)"}; case "w": return {g:0, c:null, s:"[0-6]"}; // javascript day number (0 (sunday) - 6 (saturday)) case "z": return {g:0, c:null, s:"(?:\\d{1,3}"}; // day of the year (0 - 364 (365 in leap years)) case "W": return {g:0, c:null, s:"(?:\\d{2})"}; // ISO-8601 week number (with leading zero) case "F": return {g:1, c:"m = parseInt(Date.getMonthNumber(results[" + currentGroup + "]), 10);\n", // get L10n month number s:"(" + Date.monthNames.join("|") + ")"}; case "m": return {g:1, c:"m = parseInt(results[" + currentGroup + "], 10) - 1;\n", s:"(\\d{2})"}; // month number with leading zeros (01 - 12) case "M": for (var a = [], i = 0; i < 12; a.push(Date.getShortMonthName(i)), ++i); // get L10n short month names return {g:1, c:"m = parseInt(Date.getMonthNumber(results[" + currentGroup + "]), 10);\n", // get L10n month number s:"(" + a.join("|") + ")"}; case "n": return {g:1, c:"m = parseInt(results[" + currentGroup + "], 10) - 1;\n", s:"(\\d{1,2})"}; // month number without leading zeros (1 - 12) case "t": return {g:0, c:null, s:"(?:\\d{2})"}; // no. of days in the month (28 - 31) case "L": return {g:0, c:null, s:"(?:1|0)"}; case "o": case "Y": return {g:1, c:"y = parseInt(results[" + currentGroup + "], 10);\n", s:"(\\d{4})"}; // 4-digit year case "y": return {g:1, c:"var ty = parseInt(results[" + currentGroup + "], 10);\n" + "y = ty > Date.y2kYear ? 1900 + ty : 2000 + ty;\n", s:"(\\d{1,2})"}; // 2-digit year case "a": return {g:1, c:"if (results[" + currentGroup + "] == 'am') {\n" + "if (h == 12) { h = 0; }\n" + "} else { if (h < 12) { h += 12; }}", s:"(am|pm)"}; case "A": return {g:1, c:"if (results[" + currentGroup + "] == 'AM') {\n" + "if (h == 12) { h = 0; }\n" + "} else { if (h < 12) { h += 12; }}", s:"(AM|PM)"}; case "g": case "G": return {g:1, c:"h = parseInt(results[" + currentGroup + "], 10);\n", s:"(\\d{1,2})"}; // 24-hr format of an hour without leading zeroes (0 - 23) case "h": case "H": return {g:1, c:"h = parseInt(results[" + currentGroup + "], 10);\n", s:"(\\d{2})"}; // 24-hr format of an hour with leading zeroes (00 - 23) case "i": return {g:1, c:"i = parseInt(results[" + currentGroup + "], 10);\n", s:"(\\d{2})"}; // minutes with leading zeros (00 - 59) case "s": return {g:1, c:"s = parseInt(results[" + currentGroup + "], 10);\n", s:"(\\d{2})"}; // seconds with leading zeros (00 - 59) case "u": return {g:1, c:"ms = parseInt(results[" + currentGroup + "], 10);\n", s:"(\\d{3})"}; // milliseconds with leading zeros (000 - 999) case "O": return {g:1, c:[ "o = results[", currentGroup, "];\n", "var sn = o.substring(0,1);\n", // get + / - sign "var hr = o.substring(1,3)*1 + Math.floor(o.substring(3,5) / 60);\n", // get hours (performs minutes-to-hour conversion also, just in case) "var mn = o.substring(3,5) % 60;\n", // get minutes "o = ((-12 <= (hr*60 + mn)/60) && ((hr*60 + mn)/60 <= 14))?\n", // -12hrs <= GMT offset <= 14hrs " (sn + String.leftPad(hr, 2, '0') + String.leftPad(mn, 2, '0')) : null;\n" ].join(""), s: "([+\-]\\d{4})"}; // GMT offset in hrs and mins case "P": return {g:1, c:[ "o = results[", currentGroup, "];\n", "var sn = o.substring(0,1);\n", // get + / - sign "var hr = o.substring(1,3)*1 + Math.floor(o.substring(4,6) / 60);\n", // get hours (performs minutes-to-hour conversion also, just in case) "var mn = o.substring(4,6) % 60;\n", // get minutes "o = ((-12 <= (hr*60 + mn)/60) && ((hr*60 + mn)/60 <= 14))?\n", // -12hrs <= GMT offset <= 14hrs " (sn + String.leftPad(hr, 2, '0') + String.leftPad(mn, 2, '0')) : null;\n" ].join(""), s: "([+\-]\\d{2}:\\d{2})"}; // GMT offset in hrs and mins (with colon separator) case "T": return {g:0, c:null, s:"[A-Z]{1,4}"}; // timezone abbrev. may be between 1 - 4 chars case "Z": return {g:1, c:"z = results[" + currentGroup + "] * 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 case "c": 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), df("P", 7)]; 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 + arr[6].s}; case "U": return {g:1, c:"u = parseInt(results[" + currentGroup + "], 10);\n", s:"(-?\\d+)"}; // leading minus sign indicates seconds before UNIX epoch default: return {g:0, c:null, s:Ext.escapeRe(character)}; }};/** * 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;};/** * Get the last 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.getLastDayOfMonth()]); //output: 'Wednesday'
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -