704 lines
No EOL
28 KiB
HTML
704 lines
No EOL
28 KiB
HTML
<html><head><title>Date.js</title><link rel="stylesheet" type="text/css" href="../resources/style.css" media="screen"/></head><body><h1>Date.js</h1><pre class="highlighted"><code><i>/**
|
|
* @class Date
|
|
*
|
|
* The date parsing and format syntax is a subset of
|
|
* <a href="http:<i>//www.php.net/date">PHP's date() <b>function</b></a>, and the formats that are</i>
|
|
* supported will provide results equivalent to their PHP versions.
|
|
*
|
|
* Following is the list of all currently supported formats:
|
|
*<pre>
|
|
Sample date:
|
|
'Wed Jan 10 2007 15:05:01 GMT-0600 (Central Standard Time)'
|
|
|
|
Format Output Description
|
|
------ ---------- --------------------------------------------------------------
|
|
d 10 Day of the month, 2 digits <b>with</b> leading zeros
|
|
D Wed A textual representation of a day, three letters
|
|
j 10 Day of the month without leading zeros
|
|
l Wednesday A full textual representation of the day of the week
|
|
S th English ordinal day of month suffix, 2 chars (use <b>with</b> j)
|
|
w 3 Numeric representation of the day of the week
|
|
z 9 The julian date, or day of the year (0-365)
|
|
W 01 ISO-8601 2-digit week number of year, weeks starting on Monday (00-52)
|
|
F January A full textual representation of the month
|
|
m 01 Numeric representation of a month, <b>with</b> leading zeros
|
|
M Jan Month name abbreviation, three letters
|
|
n 1 Numeric representation of a month, without leading zeros
|
|
t 31 Number of days <b>in</b> the given month
|
|
L 0 Whether it's a leap year (1 <b>if</b> it is a leap year, <b>else</b> 0)
|
|
Y 2007 A full numeric representation of a year, 4 digits
|
|
y 07 A two digit representation of a year
|
|
a pm Lowercase Ante meridiem and Post meridiem
|
|
A PM Uppercase Ante meridiem and Post meridiem
|
|
g 3 12-hour format of an hour without leading zeros
|
|
G 15 24-hour format of an hour without leading zeros
|
|
h 03 12-hour format of an hour <b>with</b> leading zeros
|
|
H 15 24-hour format of an hour <b>with</b> leading zeros
|
|
i 05 Minutes <b>with</b> leading zeros
|
|
s 01 Seconds, <b>with</b> leading zeros
|
|
O -0600 Difference to Greenwich time (GMT) <b>in</b> hours
|
|
T CST Timezone setting of the machine running the code
|
|
Z -21600 Timezone offset <b>in</b> seconds (negative <b>if</b> west of UTC, positive <b>if</b> east)
|
|
</pre>
|
|
*
|
|
* Example usage (note that you must escape format specifiers <b>with</b> '\\' to render them as character literals):
|
|
* <pre><code>
|
|
<b>var</b> dt = <b>new</b> Date('1/10/2007 03:05:01 PM GMT-0600');
|
|
document.write(dt.format('Y-m-d')); <i>//2007-01-10</i>
|
|
document.write(dt.format('F j, Y, g:i a')); <i>//January 10, 2007, 3:05 pm</i>
|
|
document.write(dt.format('l, \\t\\he dS of F Y h:i:s A')); <i>//Wednesday, the 10th of January 2007 03:05:01 PM</i>
|
|
</code></pre>
|
|
*
|
|
* Here are some standard date/time patterns that you might find helpful. They
|
|
* are not part of the source of Date.js, but to use them you can simply copy <b>this</b>
|
|
* block of code into any script that is included after Date.js and they will also become
|
|
* globally available on the Date object. Feel free to add or remove patterns as needed <b>in</b> your code.
|
|
* <pre><code>
|
|
Date.patterns = {
|
|
ISO8601Long:"Y-m-d H:i:s",
|
|
ISO8601Short:"Y-m-d",
|
|
ShortDate: "n/j/Y",
|
|
LongDate: "l, F d, Y",
|
|
FullDateTime: "l, F d, Y g:i:s A",
|
|
MonthDay: "F d",
|
|
ShortTime: "g:i A",
|
|
LongTime: "g:i:s A",
|
|
SortableDateTime: "Y-m-d\\TH:i:s",
|
|
UniversalSortableDateTime: "Y-m-d H:i:sO",
|
|
YearMonth: "F, Y"
|
|
};
|
|
</code></pre>
|
|
*
|
|
* Example usage:
|
|
* <pre><code>
|
|
<b>var</b> dt = <b>new</b> Date();
|
|
document.write(dt.format(Date.patterns.ShortDate));
|
|
</code></pre>
|
|
*/</i>
|
|
<i>// holder</i>
|
|
<i>/**
|
|
* Most of the date-formatting functions below are the excellent work of Baron Schwartz.
|
|
* They generate precompiled functions from date formats instead of parsing and
|
|
* processing the pattern every time you format a date. These functions are available
|
|
* on every Date object (any javascript <b>function</b>).
|
|
*
|
|
* The original article and download are here:
|
|
* http:<i>//www.xaprb.com/blog/2005/12/12/javascript-closures-<b>for</b>-runtime-efficiency/</i>
|
|
*
|
|
*/</i>
|
|
|
|
<i>// private</i>
|
|
Date.parseFunctions = {count:0};
|
|
<i>// private</i>
|
|
Date.parseRegexes = [];
|
|
<i>// private</i>
|
|
Date.formatFunctions = {count:0};
|
|
|
|
<i>// private</i>
|
|
Date.prototype.dateFormat = <b>function</b>(format) {
|
|
<b>if</b> (Date.formatFunctions[format] == null) {
|
|
Date.createNewFormat(format);
|
|
}
|
|
<b>var</b> func = Date.formatFunctions[format];
|
|
<b>return</b> this[func]();
|
|
};
|
|
|
|
|
|
<i>/**
|
|
* Formats a date given the supplied format string
|
|
* @param {String} format The format string
|
|
* @<b>return</b> {String} The formatted date
|
|
* @method
|
|
*/</i>
|
|
Date.prototype.format = Date.prototype.dateFormat;
|
|
|
|
<i>// private</i>
|
|
Date.createNewFormat = <b>function</b>(format) {
|
|
<b>var</b> funcName = "format" + Date.formatFunctions.count++;
|
|
Date.formatFunctions[format] = funcName;
|
|
<b>var</b> code = "Date.prototype." + funcName + " = <b>function</b>(){<b>return</b> ";
|
|
<b>var</b> special = false;
|
|
<b>var</b> ch = '';
|
|
<b>for</b> (<b>var</b> i = 0; i < format.length; ++i) {
|
|
ch = format.charAt(i);
|
|
<b>if</b> (!special && ch == "\\") {
|
|
special = true;
|
|
}
|
|
<b>else</b> if (special) {
|
|
special = false;
|
|
code += "'" + String.escape(ch) + "' + ";
|
|
}
|
|
<b>else</b> {
|
|
code += Date.getFormatCode(ch);
|
|
}
|
|
}
|
|
eval(code.substring(0, code.length - 3) + ";}");
|
|
};
|
|
|
|
<i>// private</i>
|
|
Date.getFormatCode = <b>function</b>(character) {
|
|
<b>switch</b> (character) {
|
|
<b>case</b> "d":
|
|
<b>return</b> "String.leftPad(<b>this</b>.getDate(), 2, '0') + ";
|
|
<b>case</b> "D":
|
|
<b>return</b> "Date.dayNames[<b>this</b>.getDay()].substring(0, 3) + ";
|
|
<b>case</b> "j":
|
|
<b>return</b> "<b>this</b>.getDate() + ";
|
|
<b>case</b> "l":
|
|
<b>return</b> "Date.dayNames[<b>this</b>.getDay()] + ";
|
|
<b>case</b> "S":
|
|
<b>return</b> "<b>this</b>.getSuffix() + ";
|
|
<b>case</b> "w":
|
|
<b>return</b> "<b>this</b>.getDay() + ";
|
|
<b>case</b> "z":
|
|
<b>return</b> "<b>this</b>.getDayOfYear() + ";
|
|
<b>case</b> "W":
|
|
<b>return</b> "<b>this</b>.getWeekOfYear() + ";
|
|
<b>case</b> "F":
|
|
<b>return</b> "Date.monthNames[<b>this</b>.getMonth()] + ";
|
|
<b>case</b> "m":
|
|
<b>return</b> "String.leftPad(<b>this</b>.getMonth() + 1, 2, '0') + ";
|
|
<b>case</b> "M":
|
|
<b>return</b> "Date.monthNames[<b>this</b>.getMonth()].substring(0, 3) + ";
|
|
<b>case</b> "n":
|
|
<b>return</b> "(<b>this</b>.getMonth() + 1) + ";
|
|
<b>case</b> "t":
|
|
<b>return</b> "<b>this</b>.getDaysInMonth() + ";
|
|
<b>case</b> "L":
|
|
<b>return</b> "(<b>this</b>.isLeapYear() ? 1 : 0) + ";
|
|
<b>case</b> "Y":
|
|
<b>return</b> "<b>this</b>.getFullYear() + ";
|
|
<b>case</b> "y":
|
|
<b>return</b> "('' + <b>this</b>.getFullYear()).substring(2, 4) + ";
|
|
<b>case</b> "a":
|
|
<b>return</b> "(<b>this</b>.getHours() < 12 ? 'am' : 'pm') + ";
|
|
<b>case</b> "A":
|
|
<b>return</b> "(<b>this</b>.getHours() < 12 ? 'AM' : 'PM') + ";
|
|
<b>case</b> "g":
|
|
<b>return</b> "((<b>this</b>.getHours() %12) ? <b>this</b>.getHours() % 12 : 12) + ";
|
|
<b>case</b> "G":
|
|
<b>return</b> "<b>this</b>.getHours() + ";
|
|
<b>case</b> "h":
|
|
<b>return</b> "String.leftPad((<b>this</b>.getHours() %12) ? <b>this</b>.getHours() % 12 : 12, 2, '0') + ";
|
|
<b>case</b> "H":
|
|
<b>return</b> "String.leftPad(<b>this</b>.getHours(), 2, '0') + ";
|
|
<b>case</b> "i":
|
|
<b>return</b> "String.leftPad(<b>this</b>.getMinutes(), 2, '0') + ";
|
|
<b>case</b> "s":
|
|
<b>return</b> "String.leftPad(<b>this</b>.getSeconds(), 2, '0') + ";
|
|
<b>case</b> "O":
|
|
<b>return</b> "<b>this</b>.getGMTOffset() + ";
|
|
<b>case</b> "T":
|
|
<b>return</b> "<b>this</b>.getTimezone() + ";
|
|
<b>case</b> "Z":
|
|
<b>return</b> "(<b>this</b>.getTimezoneOffset() * -60) + ";
|
|
<b>default</b>:
|
|
<b>return</b> "'" + String.escape(character) + "' + ";
|
|
}
|
|
};
|
|
|
|
<i>/**
|
|
* Parses the passed string using the specified format. Example Usage:
|
|
<pre><code>
|
|
<b>var</b> date = Date.parseDate('2006-12-25', 'Y-m-d');
|
|
</code></pre>
|
|
* @param {String} input The unparsed date as a string
|
|
* @param {String} format The format the date is <b>in</b>
|
|
* @<b>return</b> {Date} The parsed date
|
|
* @static
|
|
*/</i>
|
|
Date.parseDate = <b>function</b>(input, format) {
|
|
<b>if</b> (Date.parseFunctions[format] == null) {
|
|
Date.createParser(format);
|
|
}
|
|
<b>var</b> func = Date.parseFunctions[format];
|
|
<b>return</b> Date[func](input);
|
|
};
|
|
|
|
<i>// private</i>
|
|
Date.createParser = <b>function</b>(format) {
|
|
<b>var</b> funcName = "parse" + Date.parseFunctions.count++;
|
|
<b>var</b> regexNum = Date.parseRegexes.length;
|
|
<b>var</b> currentGroup = 1;
|
|
Date.parseFunctions[format] = funcName;
|
|
|
|
<b>var</b> code = "Date." + funcName + " = <b>function</b>(input){\n"
|
|
+ "<b>var</b> y = -1, m = -1, d = -1, h = -1, i = -1, s = -1;\n"
|
|
+ "<b>var</b> d = <b>new</b> Date();\n"
|
|
+ "y = d.getFullYear();\n"
|
|
+ "m = d.getMonth();\n"
|
|
+ "d = d.getDate();\n"
|
|
+ "<b>var</b> results = input.match(Date.parseRegexes[" + regexNum + "]);\n"
|
|
+ "<b>if</b> (results && results.length > 0) {";
|
|
<b>var</b> regex = "";
|
|
|
|
<b>var</b> special = false;
|
|
<b>var</b> ch = '';
|
|
<b>for</b> (<b>var</b> i = 0; i < format.length; ++i) {
|
|
ch = format.charAt(i);
|
|
<b>if</b> (!special && ch == "\\") {
|
|
special = true;
|
|
}
|
|
<b>else</b> if (special) {
|
|
special = false;
|
|
regex += String.escape(ch);
|
|
}
|
|
<b>else</b> {
|
|
<b>var</b> obj = Date.formatCodeToRegex(ch, currentGroup);
|
|
currentGroup += obj.g;
|
|
regex += obj.s;
|
|
<b>if</b> (obj.g && obj.c) {
|
|
code += obj.c;
|
|
}
|
|
}
|
|
}
|
|
|
|
code += "<b>if</b> (y > 0 && m >= 0 && d > 0 && h >= 0 && i >= 0 && s >= 0)\n"
|
|
+ "{<b>return</b> new Date(y, m, d, h, i, s);}\n"
|
|
+ "<b>else</b> if (y > 0 && m >= 0 && d > 0 && h >= 0 && i >= 0)\n"
|
|
+ "{<b>return</b> new Date(y, m, d, h, i);}\n"
|
|
+ "<b>else</b> if (y > 0 && m >= 0 && d > 0 && h >= 0)\n"
|
|
+ "{<b>return</b> new Date(y, m, d, h);}\n"
|
|
+ "<b>else</b> if (y > 0 && m >= 0 && d > 0)\n"
|
|
+ "{<b>return</b> new Date(y, m, d);}\n"
|
|
+ "<b>else</b> if (y > 0 && m >= 0)\n"
|
|
+ "{<b>return</b> new Date(y, m);}\n"
|
|
+ "<b>else</b> if (y > 0)\n"
|
|
+ "{<b>return</b> new Date(y);}\n"
|
|
+ "}<b>return</b> null;}";
|
|
|
|
Date.parseRegexes[regexNum] = <b>new</b> RegExp("^" + regex + "$");
|
|
eval(code);
|
|
};
|
|
|
|
<i>// private</i>
|
|
Date.formatCodeToRegex = <b>function</b>(character, currentGroup) {
|
|
<b>switch</b> (character) {
|
|
<b>case</b> "D":
|
|
<b>return</b> {g:0,
|
|
c:null,
|
|
s:"(?:Sun|Mon|Tue|Wed|Thu|Fri|Sat)"};
|
|
<b>case</b> "j":
|
|
<b>case</b> "d":
|
|
<b>return</b> {g:1,
|
|
c:"d = parseInt(results[" + currentGroup + "], 10);\n",
|
|
s:"(\\d{1,2})"};
|
|
<b>case</b> "l":
|
|
<b>return</b> {g:0,
|
|
c:null,
|
|
s:"(?:" + Date.dayNames.join("|") + ")"};
|
|
<b>case</b> "S":
|
|
<b>return</b> {g:0,
|
|
c:null,
|
|
s:"(?:st|nd|rd|th)"};
|
|
<b>case</b> "w":
|
|
<b>return</b> {g:0,
|
|
c:null,
|
|
s:"\\d"};
|
|
<b>case</b> "z":
|
|
<b>return</b> {g:0,
|
|
c:null,
|
|
s:"(?:\\d{1,3})"};
|
|
<b>case</b> "W":
|
|
<b>return</b> {g:0,
|
|
c:null,
|
|
s:"(?:\\d{2})"};
|
|
<b>case</b> "F":
|
|
<b>return</b> {g:1,
|
|
c:"m = parseInt(Date.monthNumbers[results[" + currentGroup + "].substring(0, 3)], 10);\n",
|
|
s:"(" + Date.monthNames.join("|") + ")"};
|
|
<b>case</b> "M":
|
|
<b>return</b> {g:1,
|
|
c:"m = parseInt(Date.monthNumbers[results[" + currentGroup + "]], 10);\n",
|
|
s:"(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)"};
|
|
<b>case</b> "n":
|
|
<b>case</b> "m":
|
|
<b>return</b> {g:1,
|
|
c:"m = parseInt(results[" + currentGroup + "], 10) - 1;\n",
|
|
s:"(\\d{1,2})"};
|
|
<b>case</b> "t":
|
|
<b>return</b> {g:0,
|
|
c:null,
|
|
s:"\\d{1,2}"};
|
|
<b>case</b> "L":
|
|
<b>return</b> {g:0,
|
|
c:null,
|
|
s:"(?:1|0)"};
|
|
<b>case</b> "Y":
|
|
<b>return</b> {g:1,
|
|
c:"y = parseInt(results[" + currentGroup + "], 10);\n",
|
|
s:"(\\d{4})"};
|
|
<b>case</b> "y":
|
|
<b>return</b> {g:1,
|
|
c:"<b>var</b> ty = parseInt(results[" + currentGroup + "], 10);\n"
|
|
+ "y = ty > Date.y2kYear ? 1900 + ty : 2000 + ty;\n",
|
|
s:"(\\d{1,2})"};
|
|
<b>case</b> "a":
|
|
<b>return</b> {g:1,
|
|
c:"<b>if</b> (results[" + currentGroup + "] == 'am') {\n"
|
|
+ "<b>if</b> (h == 12) { h = 0; }\n"
|
|
+ "} <b>else</b> { <b>if</b> (h < 12) { h += 12; }}",
|
|
s:"(am|pm)"};
|
|
<b>case</b> "A":
|
|
<b>return</b> {g:1,
|
|
c:"<b>if</b> (results[" + currentGroup + "] == 'AM') {\n"
|
|
+ "<b>if</b> (h == 12) { h = 0; }\n"
|
|
+ "} <b>else</b> { <b>if</b> (h < 12) { h += 12; }}",
|
|
s:"(AM|PM)"};
|
|
<b>case</b> "g":
|
|
<b>case</b> "G":
|
|
<b>case</b> "h":
|
|
<b>case</b> "H":
|
|
<b>return</b> {g:1,
|
|
c:"h = parseInt(results[" + currentGroup + "], 10);\n",
|
|
s:"(\\d{1,2})"};
|
|
<b>case</b> "i":
|
|
<b>return</b> {g:1,
|
|
c:"i = parseInt(results[" + currentGroup + "], 10);\n",
|
|
s:"(\\d{2})"};
|
|
<b>case</b> "s":
|
|
<b>return</b> {g:1,
|
|
c:"s = parseInt(results[" + currentGroup + "], 10);\n",
|
|
s:"(\\d{2})"};
|
|
<b>case</b> "O":
|
|
<b>return</b> {g:0,
|
|
c:null,
|
|
s:"[+-]\\d{4}"};
|
|
<b>case</b> "T":
|
|
<b>return</b> {g:0,
|
|
c:null,
|
|
s:"[A-Z]{3}"};
|
|
<b>case</b> "Z":
|
|
<b>return</b> {g:0,
|
|
c:null,
|
|
s:"[+-]\\d{1,5}"};
|
|
<b>default</b>:
|
|
<b>return</b> {g:0,
|
|
c:null,
|
|
s:String.escape(character)};
|
|
}
|
|
};
|
|
|
|
<i>/**
|
|
* Get the timezone abbreviation of the current date (equivalent to the format specifier 'T').
|
|
* @<b>return</b> {String} The abbreviated timezone name (e.g. 'CST')
|
|
*/</i>
|
|
Date.prototype.getTimezone = <b>function</b>() {
|
|
<b>return</b> this.toString().replace(
|
|
/^.*? ([A-Z]{3}) [0-9]{4}.*$/, "$1").replace(
|
|
/^.*?\(([A-Z])[a-z]+ ([A-Z])[a-z]+ ([A-Z])[a-z]+\)$/, "$1$2$3");
|
|
};
|
|
|
|
<i>/**
|
|
* Get the offset from GMT of the current date (equivalent to the format specifier 'O').
|
|
* @<b>return</b> {String} The 4-character offset string prefixed <b>with</b> + or - (e.g. '-0600')
|
|
*/</i>
|
|
Date.prototype.getGMTOffset = <b>function</b>() {
|
|
<b>return</b> (<b>this</b>.getTimezoneOffset() > 0 ? "-" : "+")
|
|
+ String.leftPad(Math.floor(<b>this</b>.getTimezoneOffset() / 60), 2, "0")
|
|
+ String.leftPad(<b>this</b>.getTimezoneOffset() % 60, 2, "0");
|
|
};
|
|
|
|
<i>/**
|
|
* Get the numeric day number of the year, adjusted <b>for</b> leap year.
|
|
* @<b>return</b> {Number} 0 through 365 (366 <b>in</b> leap years)
|
|
*/</i>
|
|
Date.prototype.getDayOfYear = <b>function</b>() {
|
|
<b>var</b> num = 0;
|
|
Date.daysInMonth[1] = <b>this</b>.isLeapYear() ? 29 : 28;
|
|
<b>for</b> (<b>var</b> i = 0; i < <b>this</b>.getMonth(); ++i) {
|
|
num += Date.daysInMonth[i];
|
|
}
|
|
<b>return</b> num + <b>this</b>.getDate() - 1;
|
|
};
|
|
|
|
<i>/**
|
|
* Get the string representation of the numeric week number of the year
|
|
* (equivalent to the format specifier 'W').
|
|
* @<b>return</b> {String} '00' through '52'
|
|
*/</i>
|
|
Date.prototype.getWeekOfYear = <b>function</b>() {
|
|
<i>// Skip to Thursday of <b>this</b> week</i>
|
|
<b>var</b> now = <b>this</b>.getDayOfYear() + (4 - <b>this</b>.getDay());
|
|
<i>// Find the first Thursday of the year</i>
|
|
<b>var</b> jan1 = <b>new</b> Date(<b>this</b>.getFullYear(), 0, 1);
|
|
<b>var</b> then = (7 - jan1.getDay() + 4);
|
|
<b>return</b> String.leftPad(((now - then) / 7) + 1, 2, "0");
|
|
};
|
|
|
|
<i>/**
|
|
* Whether or not the current date is <b>in</b> a leap year.
|
|
* @<b>return</b> {Boolean} True <b>if</b> the current date is <b>in</b> a leap year, <b>else</b> false
|
|
*/</i>
|
|
Date.prototype.isLeapYear = <b>function</b>() {
|
|
<b>var</b> year = <b>this</b>.getFullYear();
|
|
<b>return</b> ((year & 3) == 0 && (year % 100 || (year % 400 == 0 && year)));
|
|
};
|
|
|
|
<i>/**
|
|
* Get the first day of the current month, adjusted <b>for</b> leap year. The returned value
|
|
* is the numeric day index within the week (0-6) which can be used <b>in</b> conjunction <b>with</b>
|
|
* the {@link #monthNames} array to retrieve the textual day name.
|
|
* Example:
|
|
*<pre><code>
|
|
<b>var</b> dt = <b>new</b> Date('1/10/2007');
|
|
document.write(Date.dayNames[dt.getFirstDayOfMonth()]); <i>//output: 'Monday'</i>
|
|
</code></pre>
|
|
* @<b>return</b> {Number} The day number (0-6)
|
|
*/</i>
|
|
Date.prototype.getFirstDayOfMonth = <b>function</b>() {
|
|
<b>var</b> day = (<b>this</b>.getDay() - (<b>this</b>.getDate() - 1)) % 7;
|
|
<b>return</b> (day < 0) ? (day + 7) : day;
|
|
};
|
|
|
|
<i>/**
|
|
* Get the last day of the current month, adjusted <b>for</b> leap year. The returned value
|
|
* is the numeric day index within the week (0-6) which can be used <b>in</b> conjunction <b>with</b>
|
|
* the {@link #monthNames} array to retrieve the textual day name.
|
|
* Example:
|
|
*<pre><code>
|
|
<b>var</b> dt = <b>new</b> Date('1/10/2007');
|
|
document.write(Date.dayNames[dt.getLastDayOfMonth()]); <i>//output: 'Wednesday'</i>
|
|
</code></pre>
|
|
* @<b>return</b> {Number} The day number (0-6)
|
|
*/</i>
|
|
Date.prototype.getLastDayOfMonth = <b>function</b>() {
|
|
<b>var</b> day = (<b>this</b>.getDay() + (Date.daysInMonth[<b>this</b>.getMonth()] - <b>this</b>.getDate())) % 7;
|
|
<b>return</b> (day < 0) ? (day + 7) : day;
|
|
};
|
|
|
|
|
|
<i>/**
|
|
* Get a Date of the first day of <b>this</b> date's month
|
|
* @<b>return</b> {Date}
|
|
*/</i>
|
|
Date.prototype.getFirstDateOfMonth = <b>function</b>() {
|
|
<b>return</b> new Date(<b>this</b>.getFullYear(), <b>this</b>.getMonth(), 1);
|
|
};
|
|
|
|
<i>/**
|
|
* Get a Date of the late day of <b>this</b> date's month
|
|
* @<b>return</b> {Date}
|
|
*/</i>
|
|
Date.prototype.getLastDateOfMonth = <b>function</b>() {
|
|
<b>return</b> new Date(<b>this</b>.getFullYear(), <b>this</b>.getMonth(), <b>this</b>.getDaysInMonth());
|
|
};
|
|
<i>/**
|
|
* Get the number of days <b>in</b> the current month, adjusted <b>for</b> leap year.
|
|
* @<b>return</b> {Number} The number of days <b>in</b> the month
|
|
*/</i>
|
|
Date.prototype.getDaysInMonth = <b>function</b>() {
|
|
Date.daysInMonth[1] = <b>this</b>.isLeapYear() ? 29 : 28;
|
|
<b>return</b> Date.daysInMonth[<b>this</b>.getMonth()];
|
|
};
|
|
|
|
<i>/**
|
|
* Get the English ordinal suffix of the current day (equivalent to the format specifier 'S').
|
|
* @<b>return</b> {String} 'st, 'nd', 'rd' or 'th'
|
|
*/</i>
|
|
Date.prototype.getSuffix = <b>function</b>() {
|
|
<b>switch</b> (<b>this</b>.getDate()) {
|
|
<b>case</b> 1:
|
|
<b>case</b> 21:
|
|
<b>case</b> 31:
|
|
<b>return</b> "st";
|
|
<b>case</b> 2:
|
|
<b>case</b> 22:
|
|
<b>return</b> "nd";
|
|
<b>case</b> 3:
|
|
<b>case</b> 23:
|
|
<b>return</b> "rd";
|
|
<b>default</b>:
|
|
<b>return</b> "th";
|
|
}
|
|
};
|
|
|
|
<i>// private</i>
|
|
Date.daysInMonth = [31,28,31,30,31,30,31,31,30,31,30,31];
|
|
|
|
<i>/**
|
|
* An array of textual month names.
|
|
* Override these values <b>for</b> international dates, <b>for</b> example...
|
|
* Date.monthNames = ['JanInYourLang', 'FebInYourLang', ...];
|
|
* @type Array
|
|
* @static
|
|
*/</i>
|
|
Date.monthNames =
|
|
["January",
|
|
"February",
|
|
"March",
|
|
"April",
|
|
"May",
|
|
"June",
|
|
"July",
|
|
"August",
|
|
"September",
|
|
"October",
|
|
"November",
|
|
"December"];
|
|
|
|
<i>/**
|
|
* An array of textual day names.
|
|
* Override these values <b>for</b> international dates, <b>for</b> example...
|
|
* Date.dayNames = ['SundayInYourLang', 'MondayInYourLang', ...];
|
|
* @type Array
|
|
* @static
|
|
*/</i>
|
|
Date.dayNames =
|
|
["Sunday",
|
|
"Monday",
|
|
"Tuesday",
|
|
"Wednesday",
|
|
"Thursday",
|
|
"Friday",
|
|
"Saturday"];
|
|
|
|
<i>// private</i>
|
|
Date.y2kYear = 50;
|
|
<i>// private</i>
|
|
Date.monthNumbers = {
|
|
Jan:0,
|
|
Feb:1,
|
|
Mar:2,
|
|
Apr:3,
|
|
May:4,
|
|
Jun:5,
|
|
Jul:6,
|
|
Aug:7,
|
|
Sep:8,
|
|
Oct:9,
|
|
Nov:10,
|
|
Dec:11};
|
|
|
|
<i>/**
|
|
* Creates and returns a <b>new</b> Date instance <b>with</b> the exact same date value as the called instance.
|
|
* Dates are copied and passed by reference, so <b>if</b> a copied date variable is modified later, the original
|
|
* variable will also be changed. When the intention is to create a <b>new</b> variable that will not
|
|
* modify the original instance, you should create a clone.
|
|
*
|
|
* Example of correctly cloning a date:
|
|
* <pre><code>
|
|
<i>//wrong way:</i>
|
|
<b>var</b> orig = <b>new</b> Date('10/1/2006');
|
|
<b>var</b> copy = orig;
|
|
copy.setDate(5);
|
|
document.write(orig); <i>//returns 'Thu Oct 05 2006'!</i>
|
|
|
|
<i>//correct way:</i>
|
|
<b>var</b> orig = <b>new</b> Date('10/1/2006');
|
|
<b>var</b> copy = orig.clone();
|
|
copy.setDate(5);
|
|
document.write(orig); <i>//returns 'Thu Oct 01 2006'</i>
|
|
</code></pre>
|
|
* @<b>return</b> {Date} The <b>new</b> Date instance
|
|
*/</i>
|
|
Date.prototype.clone = <b>function</b>() {
|
|
<b>return</b> new Date(<b>this</b>.getTime());
|
|
};
|
|
|
|
<i>/**
|
|
* Clears any time information from <b>this</b> date
|
|
@param {Boolean} clone true to create a clone of <b>this</b> date, clear the time and <b>return</b> it
|
|
@<b>return</b> {Date} <b>this</b> or the clone
|
|
*/</i>
|
|
Date.prototype.clearTime = <b>function</b>(clone){
|
|
<b>if</b>(clone){
|
|
<b>return</b> this.clone().clearTime();
|
|
}
|
|
<b>this</b>.setHours(0);
|
|
<b>this</b>.setMinutes(0);
|
|
<b>this</b>.setSeconds(0);
|
|
<b>this</b>.setMilliseconds(0);
|
|
<b>return</b> this;
|
|
};
|
|
|
|
<i>// private</i>
|
|
<i>// safari setMonth is broken</i>
|
|
<b>if</b>(Ext.isSafari){
|
|
Date.brokenSetMonth = Date.prototype.setMonth;
|
|
Date.prototype.setMonth = <b>function</b>(num){
|
|
<b>if</b>(num <= -1){
|
|
<b>var</b> n = Math.ceil(-num);
|
|
<b>var</b> back_year = Math.ceil(n/12);
|
|
<b>var</b> month = (n % 12) ? 12 - n % 12 : 0 ;
|
|
<b>this</b>.setFullYear(<b>this</b>.getFullYear() - back_year);
|
|
<b>return</b> Date.brokenSetMonth.call(<b>this</b>, month);
|
|
} <b>else</b> {
|
|
<b>return</b> Date.brokenSetMonth.apply(<b>this</b>, arguments);
|
|
}
|
|
};
|
|
}
|
|
|
|
<i>/** Date interval constant @static @type String */</i>
|
|
Date.MILLI = "ms";
|
|
<i>/** Date interval constant @static @type String */</i>
|
|
Date.SECOND = "s";
|
|
<i>/** Date interval constant @static @type String */</i>
|
|
Date.MINUTE = "mi";
|
|
<i>/** Date interval constant @static @type String */</i>
|
|
Date.HOUR = "h";
|
|
<i>/** Date interval constant @static @type String */</i>
|
|
Date.DAY = "d";
|
|
<i>/** Date interval constant @static @type String */</i>
|
|
Date.MONTH = "mo";
|
|
<i>/** Date interval constant @static @type String */</i>
|
|
Date.YEAR = "y";
|
|
|
|
<i>/**
|
|
* Provides a convenient method of performing basic date arithmetic. This method
|
|
* does not modify the Date instance being called - it creates and returns
|
|
* a <b>new</b> Date instance containing the resulting date value.
|
|
*
|
|
* Examples:
|
|
* <pre><code>
|
|
<i>//Basic usage:</i>
|
|
<b>var</b> dt = <b>new</b> Date('10/29/2006').add(Date.DAY, 5);
|
|
document.write(dt); <i>//returns 'Fri Oct 06 2006 00:00:00'</i>
|
|
|
|
<i>//Negative values will subtract correctly:</i>
|
|
<b>var</b> dt2 = <b>new</b> Date('10/1/2006').add(Date.DAY, -5);
|
|
document.write(dt2); <i>//returns 'Tue Sep 26 2006 00:00:00'</i>
|
|
|
|
<i>//You can even chain several calls together <b>in</b> one line!</i>
|
|
<b>var</b> dt3 = <b>new</b> Date('10/1/2006').add(Date.DAY, 5).add(Date.HOUR, 8).add(Date.MINUTE, -30);
|
|
document.write(dt3); <i>//returns 'Fri Oct 06 2006 07:30:00'</i>
|
|
</code></pre>
|
|
*
|
|
* @param {String} interval A valid date interval enum value
|
|
* @param {Number} value The amount to add to the current date
|
|
* @<b>return</b> {Date} The <b>new</b> Date instance
|
|
*/</i>
|
|
Date.prototype.add = <b>function</b>(interval, value){
|
|
<b>var</b> d = <b>this</b>.clone();
|
|
<b>if</b> (!interval || value === 0) <b>return</b> d;
|
|
<b>switch</b>(interval.toLowerCase()){
|
|
<b>case</b> Date.MILLI:
|
|
d.setMilliseconds(<b>this</b>.getMilliseconds() + value);
|
|
<b>break</b>;
|
|
<b>case</b> Date.SECOND:
|
|
d.setSeconds(<b>this</b>.getSeconds() + value);
|
|
<b>break</b>;
|
|
<b>case</b> Date.MINUTE:
|
|
d.setMinutes(<b>this</b>.getMinutes() + value);
|
|
<b>break</b>;
|
|
<b>case</b> Date.HOUR:
|
|
d.setHours(<b>this</b>.getHours() + value);
|
|
<b>break</b>;
|
|
<b>case</b> Date.DAY:
|
|
d.setDate(<b>this</b>.getDate() + value);
|
|
<b>break</b>;
|
|
<b>case</b> Date.MONTH:
|
|
<b>var</b> day = <b>this</b>.getDate();
|
|
<b>if</b>(day > 28){
|
|
day = Math.min(day, <b>this</b>.getFirstDateOfMonth().add('mo', value).getLastDateOfMonth().getDate());
|
|
}
|
|
d.setDate(day);
|
|
d.setMonth(<b>this</b>.getMonth() + value);
|
|
<b>break</b>;
|
|
<b>case</b> Date.YEAR:
|
|
d.setFullYear(<b>this</b>.getFullYear() + value);
|
|
<b>break</b>;
|
|
}
|
|
<b>return</b> d;
|
|
};</code></pre><hr><div style="font-size:10px;text-align:center;color:gray;">Ext - Copyright © 2006-2007 Ext JS, LLC<br />All rights reserved.</div>
|
|
</body></html> |