Changeset 7143 for trunk/prototype/plugins/datejs/sugarpak-debug.js
- Timestamp:
- 08/31/12 18:00:30 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/prototype/plugins/datejs/sugarpak-debug.js
r5341 r7143 1 1 /** 2 * Version: 1.0 Alpha-1 3 * Build Date: 12-Nov-2007 4 * Copyright (c) 2006-2007, Coolite Inc. (http://www.coolite.com/). All rights reserved. 5 * License: Licensed under The MIT License. See license.txt and http://www.datejs.com/license/. 6 * Website: http://www.datejs.com/ or http://www.coolite.com/datejs/ 2 * @version: 1.0 Alpha-1 3 * @author: Coolite Inc. http://www.coolite.com/ 4 * @date: 2008-04-13 5 * @copyright: Copyright (c) 2006-2008, Coolite Inc. (http://www.coolite.com/). All rights reserved. 6 * @license: Licensed under The MIT License. See license.txt and http://www.datejs.com/license/. 7 * @website: http://www.datejs.com/ 7 8 */ 8 9 … … 13 14 */ 14 15 15 /**16 * Gets a date that is set to the current date and time.17 * @return {Date} The current date and time.18 */19 Date.now = function () {20 return new Date();21 };22 23 /**24 * Gets a date that is set to the current date. The time is set to the start of the day (00:00 or 12:00 AM).25 * @return {Date} The current date.26 */27 Date.today = function () {28 return Date.now().clearTime();29 };30 31 // private32 Date.prototype._orient = +1;33 34 /**35 * Moves the date to the next instance of a date as specified by a trailing date element function (eg. .day(), .month()), month name function (eg. .january(), .jan()) or day name function (eg. .friday(), fri()).36 * Example37 <pre><code>38 Date.today().next().friday();39 Date.today().next().fri();40 Date.today().next().march();41 Date.today().next().mar();42 Date.today().next().week();43 </code></pre>44 *45 * @return {Date} this46 */47 Date.prototype.next = function () {48 this._orient = +1;49 return this;50 };51 52 /**53 * Moves the date to the previous instance of a date as specified by a trailing date element function (eg. .day(), .month()), month name function (eg. .january(), .jan()) or day name function (eg. .friday(), fri()).54 * Example55 <pre><code>56 Date.today().last().friday();57 Date.today().last().fri();58 Date.today().last().march();59 Date.today().last().mar();60 Date.today().last().week();61 </code></pre>62 *63 * @return {Date} this64 */65 Date.prototype.last = Date.prototype.prev = Date.prototype.previous = function () {66 this._orient = -1;67 return this;68 };69 70 // private71 Date.prototype._is = false;72 73 /**74 * Performs a equality check when followed by either a month name or day name function.75 * Example76 <pre><code>77 Date.today().is().friday(); // true|false78 Date.today().is().fri();79 Date.today().is().march();80 Date.today().is().mar();81 </code></pre>82 *83 * @return {bool} true|false84 */85 Date.prototype.is = function () {86 this._is = true;87 return this;88 };89 90 // private91 Number.prototype._dateElement = "day";92 93 /**94 * Creates a new Date (Date.now()) and adds this (Number) to the date based on the preceding date element function (eg. second|minute|hour|day|month|year).95 * Example96 <pre><code>97 // Undeclared Numbers must be wrapped with parentheses. Requirment of JavaScript.98 (3).days().fromNow();99 (6).months().fromNow();100 101 // Declared Number variables do not require parentheses.102 var n = 6;103 n.months().fromNow();104 </code></pre>105 *106 * @return {Date} A new Date instance107 */108 Number.prototype.fromNow = function () {109 var c = {};110 c[this._dateElement] = this;111 return Date.now().add(c);112 };113 114 /**115 * Creates a new Date (Date.now()) and subtract this (Number) from the date based on the preceding date element function (eg. second|minute|hour|day|month|year).116 * Example117 <pre><code>118 // Undeclared Numbers must be wrapped with parentheses. Requirment of JavaScript.119 (3).days().ago();120 (6).months().ago();121 122 // Declared Number variables do not require parentheses.123 var n = 6;124 n.months().ago();125 </code></pre>126 *127 * @return {Date} A new Date instance128 */129 Number.prototype.ago = function () {130 var c = {};131 c[this._dateElement] = this * -1;132 return Date.now().add(c);133 };134 135 // Build dynamic date element, month name and day name functions.136 16 (function () { 137 var $D = Date.prototype, $N = Number.prototype; 138 139 /* Do NOT modify the following string tokens. These tokens are used to build dynamic functions. */ 17 var $D = Date, $P = $D.prototype, $C = $D.CultureInfo, $N = Number.prototype; 18 19 // private 20 $P._orient = +1; 21 22 // private 23 $P._nth = null; 24 25 // private 26 $P._is = false; 27 28 // private 29 $P._same = false; 30 31 // private 32 $P._isSecond = false; 33 34 // private 35 $N._dateElement = "day"; 36 37 /** 38 * Moves the date to the next instance of a date as specified by the subsequent date element function (eg. .day(), .month()), month name function (eg. .january(), .jan()) or day name function (eg. .friday(), fri()). 39 * Example 40 <pre><code> 41 Date.today().next().friday(); 42 Date.today().next().fri(); 43 Date.today().next().march(); 44 Date.today().next().mar(); 45 Date.today().next().week(); 46 </code></pre> 47 * 48 * @return {Date} date 49 */ 50 $P.next = function () { 51 this._orient = +1; 52 return this; 53 }; 54 55 /** 56 * Creates a new Date (Date.today()) and moves the date to the next instance of the date as specified by the subsequent date element function (eg. .day(), .month()), month name function (eg. .january(), .jan()) or day name function (eg. .friday(), fri()). 57 * Example 58 <pre><code> 59 Date.next().friday(); 60 Date.next().fri(); 61 Date.next().march(); 62 Date.next().mar(); 63 Date.next().week(); 64 </code></pre> 65 * 66 * @return {Date} date 67 */ 68 $D.next = function () { 69 return $D.today().next(); 70 }; 71 72 /** 73 * Moves the date to the previous instance of a date as specified by the subsequent date element function (eg. .day(), .month()), month name function (eg. .january(), .jan()) or day name function (eg. .friday(), fri()). 74 * Example 75 <pre><code> 76 Date.today().last().friday(); 77 Date.today().last().fri(); 78 Date.today().last().march(); 79 Date.today().last().mar(); 80 Date.today().last().week(); 81 </code></pre> 82 * 83 * @return {Date} date 84 */ 85 $P.last = $P.prev = $P.previous = function () { 86 this._orient = -1; 87 return this; 88 }; 89 90 /** 91 * Creates a new Date (Date.today()) and moves the date to the previous instance of the date as specified by the subsequent date element function (eg. .day(), .month()), month name function (eg. .january(), .jan()) or day name function (eg. .friday(), fri()). 92 * Example 93 <pre><code> 94 Date.last().friday(); 95 Date.last().fri(); 96 Date.previous().march(); 97 Date.prev().mar(); 98 Date.last().week(); 99 </code></pre> 100 * 101 * @return {Date} date 102 */ 103 $D.last = $D.prev = $D.previous = function () { 104 return $D.today().last(); 105 }; 106 107 /** 108 * Performs a equality check when followed by either a month name, day name or .weekday() function. 109 * Example 110 <pre><code> 111 Date.today().is().friday(); // true|false 112 Date.today().is().fri(); 113 Date.today().is().march(); 114 Date.today().is().mar(); 115 </code></pre> 116 * 117 * @return {Boolean} true|false 118 */ 119 $P.is = function () { 120 this._is = true; 121 return this; 122 }; 123 124 /** 125 * Determines if two date objects occur on/in exactly the same instance of the subsequent date part function. 126 * The function .same() must be followed by a date part function (example: .day(), .month(), .year(), etc). 127 * 128 * An optional Date can be passed in the date part function. If now date is passed as a parameter, 'Now' is used. 129 * 130 * The following example demonstrates how to determine if two dates fall on the exact same day. 131 * 132 * Example 133 <pre><code> 134 var d1 = Date.today(); // today at 00:00 135 var d2 = new Date(); // exactly now. 136 137 // Do they occur on the same day? 138 d1.same().day(d2); // true 139 140 // Do they occur on the same hour? 141 d1.same().hour(d2); // false, unless d2 hour is '00' (midnight). 142 143 // What if it's the same day, but one year apart? 144 var nextYear = Date.today().add(1).year(); 145 146 d1.same().day(nextYear); // false, because the dates must occur on the exact same day. 147 </code></pre> 148 * 149 * Scenario: Determine if a given date occurs during some week period 2 months from now. 150 * 151 * Example 152 <pre><code> 153 var future = Date.today().add(2).months(); 154 return someDate.same().week(future); // true|false; 155 </code></pre> 156 * 157 * @return {Boolean} true|false 158 */ 159 $P.same = function () { 160 this._same = true; 161 this._isSecond = false; 162 return this; 163 }; 164 165 /** 166 * Determines if the current date/time occurs during Today. Must be preceded by the .is() function. 167 * Example 168 <pre><code> 169 someDate.is().today(); // true|false 170 new Date().is().today(); // true 171 Date.today().is().today();// true 172 Date.today().add(-1).day().is().today(); // false 173 </code></pre> 174 * 175 * @return {Boolean} true|false 176 */ 177 $P.today = function () { 178 return this.same().day(); 179 }; 180 181 /** 182 * Determines if the current date is a weekday. This function must be preceded by the .is() function. 183 * Example 184 <pre><code> 185 Date.today().is().weekday(); // true|false 186 </code></pre> 187 * 188 * @return {Boolean} true|false 189 */ 190 $P.weekday = function () { 191 if (this._is) { 192 this._is = false; 193 return (!this.is().sat() && !this.is().sun()); 194 } 195 return false; 196 }; 197 198 /** 199 * Sets the Time of the current Date instance. A string "6:15 pm" or config object {hour:18, minute:15} are accepted. 200 * Example 201 <pre><code> 202 // Set time to 6:15pm with a String 203 Date.today().at("6:15pm"); 204 205 // Set time to 6:15pm with a config object 206 Date.today().at({hour:18, minute:15}); 207 </code></pre> 208 * 209 * @return {Date} date 210 */ 211 $P.at = function (time) { 212 return (typeof time === "string") ? $D.parse(this.toString("d") + " " + time) : this.set(time); 213 }; 214 215 /** 216 * Creates a new Date() and adds this (Number) to the date based on the preceding date element function (eg. second|minute|hour|day|month|year). 217 * Example 218 <pre><code> 219 // Undeclared Numbers must be wrapped with parentheses. Requirment of JavaScript. 220 (3).days().fromNow(); 221 (6).months().fromNow(); 222 223 // Declared Number variables do not require parentheses. 224 var n = 6; 225 n.months().fromNow(); 226 </code></pre> 227 * 228 * @return {Date} A new Date instance 229 */ 230 $N.fromNow = $N.after = function (date) { 231 var c = {}; 232 c[this._dateElement] = this; 233 return ((!date) ? new Date() : date.clone()).add(c); 234 }; 235 236 /** 237 * Creates a new Date() and subtract this (Number) from the date based on the preceding date element function (eg. second|minute|hour|day|month|year). 238 * Example 239 <pre><code> 240 // Undeclared Numbers must be wrapped with parentheses. Requirment of JavaScript. 241 (3).days().ago(); 242 (6).months().ago(); 243 244 // Declared Number variables do not require parentheses. 245 var n = 6; 246 n.months().ago(); 247 </code></pre> 248 * 249 * @return {Date} A new Date instance 250 */ 251 $N.ago = $N.before = function (date) { 252 var c = {}; 253 c[this._dateElement] = this * -1; 254 return ((!date) ? new Date() : date.clone()).add(c); 255 }; 256 257 // Do NOT modify the following string tokens. These tokens are used to build dynamic functions. 258 // All culture-specific strings can be found in the CultureInfo files. See /trunk/src/globalization/. 140 259 var dx = ("sunday monday tuesday wednesday thursday friday saturday").split(/\s/), 141 260 mx = ("january february march april may june july august september october november december").split(/\s/), 142 261 px = ("Millisecond Second Minute Hour Day Week Month Year").split(/\s/), 262 pxf = ("Milliseconds Seconds Minutes Hours Date Week Month FullYear").split(/\s/), 263 nth = ("final first second third fourth fifth").split(/\s/), 143 264 de; 144 265 266 /** 267 * Returns an object literal of all the date parts. 268 * Example 269 <pre><code> 270 var o = new Date().toObject(); 271 272 // { year: 2008, month: 4, week: 20, day: 13, hour: 18, minute: 9, second: 32, millisecond: 812 } 273 274 // The object properties can be referenced directly from the object. 275 276 alert(o.day); // alerts "13" 277 alert(o.year); // alerts "2008" 278 </code></pre> 279 * 280 * @return {Date} An object literal representing the original date object. 281 */ 282 $P.toObject = function () { 283 var o = {}; 284 for (var i = 0; i < px.length; i++) { 285 o[px[i].toLowerCase()] = this["get" + pxf[i]](); 286 } 287 return o; 288 }; 289 290 /** 291 * Returns a date created from an object literal. Ignores the .week property if set in the config. 292 * Example 293 <pre><code> 294 var o = new Date().toObject(); 295 296 return Date.fromObject(o); // will return the same date. 297 298 var o2 = {month: 1, day: 20, hour: 18}; // birthday party! 299 Date.fromObject(o2); 300 </code></pre> 301 * 302 * @return {Date} An object literal representing the original date object. 303 */ 304 $D.fromObject = function(config) { 305 config.week = null; 306 return Date.today().set(config); 307 }; 308 145 309 // Create day name functions and abbreviated day name functions (eg. monday(), friday(), fri()). 146 310 var df = function (n) { … … 150 314 return this.getDay() == n; 151 315 } 316 if (this._nth !== null) { 317 // If the .second() function was called earlier, remove the _orient 318 // from the date, and then continue. 319 // This is required because 'second' can be used in two different context. 320 // 321 // Example 322 // 323 // Date.today().add(1).second(); 324 // Date.march().second().monday(); 325 // 326 // Things get crazy with the following... 327 // Date.march().add(1).second().second().monday(); // but it works!! 328 // 329 if (this._isSecond) { 330 this.addSeconds(this._orient * -1); 331 } 332 // make sure we reset _isSecond 333 this._isSecond = false; 334 335 var ntemp = this._nth; 336 this._nth = null; 337 var temp = this.clone().moveToLastDayOfMonth(); 338 this.moveToNthOccurrence(n, ntemp); 339 if (this > temp) { 340 throw new RangeError($D.getDayName(n) + " does not occur " + ntemp + " times in the month of " + $D.getMonthName(temp.getMonth()) + " " + temp.getFullYear() + "."); 341 } 342 return this; 343 } 152 344 return this.moveToDayOfWeek(n, this._orient); 153 345 }; 154 346 }; 155 347 156 for (var i = 0 ; i < dx.length ; i++) { 157 $D[dx[i]] = $D[dx[i].substring(0, 3)] = df(i); 348 var sdf = function (n) { 349 return function () { 350 var t = $D.today(), shift = n - t.getDay(); 351 if (n === 0 && $C.firstDayOfWeek === 1 && t.getDay() !== 0) { 352 shift = shift + 7; 353 } 354 return t.addDays(shift); 355 }; 356 }; 357 358 for (var i = 0; i < dx.length; i++) { 359 // Create constant static Day Name variables. Example: Date.MONDAY or Date.MON 360 $D[dx[i].toUpperCase()] = $D[dx[i].toUpperCase().substring(0, 3)] = i; 361 362 // Create Day Name functions. Example: Date.monday() or Date.mon() 363 $D[dx[i]] = $D[dx[i].substring(0, 3)] = sdf(i); 364 365 // Create Day Name instance functions. Example: Date.today().next().monday() 366 $P[dx[i]] = $P[dx[i].substring(0, 3)] = df(i); 158 367 } 159 368 … … 169 378 }; 170 379 171 for (var j = 0 ; j < mx.length ; j++) { 172 $D[mx[j]] = $D[mx[j].substring(0, 3)] = mf(j); 380 var smf = function (n) { 381 return function () { 382 return $D.today().set({ month: n, day: 1 }); 383 }; 384 }; 385 386 for (var j = 0; j < mx.length; j++) { 387 // Create constant static Month Name variables. Example: Date.MARCH or Date.MAR 388 $D[mx[j].toUpperCase()] = $D[mx[j].toUpperCase().substring(0, 3)] = j; 389 390 // Create Month Name functions. Example: Date.march() or Date.mar() 391 $D[mx[j]] = $D[mx[j].substring(0, 3)] = smf(j); 392 393 // Create Month Name instance functions. Example: Date.today().next().march() 394 $P[mx[j]] = $P[mx[j].substring(0, 3)] = mf(j); 173 395 } 174 396 175 397 // Create date element functions and plural date element functions used with Date (eg. day(), days(), months()). 176 var ef = function (j) { 398 var ef = function (j) { 177 399 return function () { 178 if (j.substring(j.length - 1) != "s") { 400 // if the .second() function was called earlier, the _orient 401 // has alread been added. Just return this and reset _isSecond. 402 if (this._isSecond) { 403 this._isSecond = false; 404 return this; 405 } 406 407 if (this._same) { 408 this._same = this._is = false; 409 var o1 = this.toObject(), 410 o2 = (arguments[0] || new Date()).toObject(), 411 v = "", 412 k = j.toLowerCase(); 413 414 for (var m = (px.length - 1); m > -1; m--) { 415 v = px[m].toLowerCase(); 416 if (o1[v] != o2[v]) { 417 return false; 418 } 419 if (k == v) { 420 break; 421 } 422 } 423 return true; 424 } 425 426 if (j.substring(j.length - 1) != "s") { 179 427 j += "s"; 180 428 } 181 return this["add" + j](this._orient); 182 }; 183 }; 184 185 // Create date element functions and plural date element functions used with Number (eg. day(), days(), months()).429 return this["add" + j](this._orient); 430 }; 431 }; 432 433 186 434 var nf = function (n) { 187 435 return function () { … … 190 438 }; 191 439 }; 192 193 for (var k = 0 ; k < px.length; k++) {440 441 for (var k = 0; k < px.length; k++) { 194 442 de = px[k].toLowerCase(); 195 $D[de] = $D[de + "s"] = ef(px[k]); 443 444 // Create date element functions and plural date element functions used with Date (eg. day(), days(), months()). 445 $P[de] = $P[de + "s"] = ef(px[k]); 446 447 // Create date element functions and plural date element functions used with Number (eg. day(), days(), months()). 196 448 $N[de] = $N[de + "s"] = nf(de); 197 449 } 450 451 $P._ss = ef("Second"); 452 453 var nthfn = function (n) { 454 return function (dayOfWeek) { 455 if (this._same) { 456 return this._ss(arguments[0]); 457 } 458 if (dayOfWeek || dayOfWeek === 0) { 459 return this.moveToNthOccurrence(dayOfWeek, n); 460 } 461 this._nth = n; 462 463 // if the operator is 'second' add the _orient, then deal with it later... 464 if (n === 2 && (dayOfWeek === undefined || dayOfWeek === null)) { 465 this._isSecond = true; 466 return this.addSeconds(this._orient); 467 } 468 return this; 469 }; 470 }; 471 472 for (var l = 0; l < nth.length; l++) { 473 $P[nth[l]] = (l === 0) ? nthfn(-1) : nthfn(l); 474 } 198 475 }()); 199 200 /**201 * Converts the current date instance into a JSON string value.202 * @return {String} JSON string of date203 */204 Date.prototype.toJSONString = function () {205 return this.toString("yyyy-MM-ddThh:mm:ssZ");206 };207 208 /**209 * Converts the current date instance to a string using the culture specific shortDatePattern.210 * @return {String} A string formatted as per the culture specific shortDatePattern211 */212 Date.prototype.toShortDateString = function () {213 return this.toString(Date.CultureInfo.formatPatterns.shortDatePattern);214 };215 216 /**217 * Converts the current date instance to a string using the culture specific longDatePattern.218 * @return {String} A string formatted as per the culture specific longDatePattern219 */220 Date.prototype.toLongDateString = function () {221 return this.toString(Date.CultureInfo.formatPatterns.longDatePattern);222 };223 224 /**225 * Converts the current date instance to a string using the culture specific shortTimePattern.226 * @return {String} A string formatted as per the culture specific shortTimePattern227 */228 Date.prototype.toShortTimeString = function () {229 return this.toString(Date.CultureInfo.formatPatterns.shortTimePattern);230 };231 232 /**233 * Converts the current date instance to a string using the culture specific longTimePattern.234 * @return {String} A string formatted as per the culture specific longTimePattern235 */236 Date.prototype.toLongTimeString = function () {237 return this.toString(Date.CultureInfo.formatPatterns.longTimePattern);238 };239 240 /**241 * Get the ordinal suffix of the current day.242 * @return {String} "st, "nd", "rd" or "th"243 */244 Date.prototype.getOrdinal = function () {245 switch (this.getDate()) {246 case 1:247 case 21:248 case 31:249 return "st";250 case 2:251 case 22:252 return "nd";253 case 3:254 case 23:255 return "rd";256 default:257 return "th";258 }259 };
Note: See TracChangeset
for help on using the changeset viewer.