2 * Date prototype extensions. Doesn't depend on any
3 * other code. Doens't overwrite existing methods.
5 * Adds dayNames, abbrDayNames, monthNames and abbrMonthNames static properties and isLeapYear,
6 * isWeekend, isWeekDay, getDaysInMonth, getDayName, getMonthName, getDayOfYear, getWeekOfYear,
7 * setDayOfYear, addYears, addMonths, addDays, addHours, addMinutes, addSeconds methods
9 * Copyright (c) 2006 Jörn Zaefferer and Brandon Aaron (brandon.aaron@gmail.com || http://brandonaaron.net)
11 * Additional methods and properties added by Kelvin Luck: firstDayOfWeek, dateFormat, zeroTime, asString, fromString -
12 * I've added my name to these methods so you know who to blame if they are broken!
14 * Dual licensed under the MIT and GPL licenses:
15 * http://www.opensource.org/licenses/mit-license.php
16 * http://www.gnu.org/licenses/gpl.html
21 * An Array of day names starting with Sunday.
23 * @example dayNames[0]
28 * @cat Plugins/Methods/Date
30 //Date.dayNames = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
33 * An Array of abbreviated day names starting with Sun.
35 * @example abbrDayNames[0]
40 * @cat Plugins/Methods/Date
42 //Date.abbrDayNames = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
45 * An Array of month names starting with Janurary.
47 * @example monthNames[0]
52 * @cat Plugins/Methods/Date
54 //Date.monthNames = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
57 * An Array of abbreviated month names starting with Jan.
59 * @example abbrMonthNames[0]
64 * @cat Plugins/Methods/Date
66 //Date.abbrMonthNames = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
69 * The first day of the week for this locale.
71 * @name firstDayOfWeek
73 * @cat Plugins/Methods/Date
76 //Date.firstDayOfWeek = 1;
79 * The format that string dates should be represented as (e.g. 'dd/mm/yyyy' for UK, 'mm/dd/yyyy' for US, 'yyyy-mm-dd' for Unicode etc).
83 * @cat Plugins/Methods/Date
86 //Date.format = 'dd/mm/yyyy';
87 //Date.format = 'mm/dd/yyyy';
88 //Date.format = 'yyyy-mm-dd';
89 //Date.format = 'dd mmm yy';
92 * The first two numbers in the century to be used when decoding a two digit year. Since a two digit year is ambiguous (and date.setYear
93 * only works with numbers < 99 and so doesn't allow you to set years after 2000) we need to use this to disambiguate the two digit year codes.
97 * @cat Plugins/Methods/Date
100 Date.fullYearStart = '20';
105 * Adds a given method under the given name
106 * to the Date prototype if it doesn't
111 function add(name, method) {
112 if( !Date.prototype[name] ) {
113 Date.prototype[name] = method;
118 * Checks if the year is a leap year.
120 * @example var dtm = new Date("01/12/2008");
126 * @cat Plugins/Methods/Date
128 add("isLeapYear", function() {
129 var y = this.getFullYear();
130 return (y%4==0 && y%100!=0) || y%400==0;
134 * Checks if the day is a weekend day (Sat or Sun).
136 * @example var dtm = new Date("01/12/2008");
142 * @cat Plugins/Methods/Date
144 add("isWeekend", function() {
145 return this.getDay()==0 || this.getDay()==6;
149 * Check if the day is a day of the week (Mon-Fri)
151 * @example var dtm = new Date("01/12/2008");
157 * @cat Plugins/Methods/Date
159 add("isWeekDay", function() {
160 return !this.isWeekend();
164 * Gets the number of days in the month.
166 * @example var dtm = new Date("01/12/2008");
167 * dtm.getDaysInMonth();
170 * @name getDaysInMonth
172 * @cat Plugins/Methods/Date
174 add("getDaysInMonth", function() {
175 return [31,(this.isLeapYear() ? 29:28),31,30,31,30,31,31,30,31,30,31][this.getMonth()];
179 * Gets the name of the day.
181 * @example var dtm = new Date("01/12/2008");
185 * @example var dtm = new Date("01/12/2008");
186 * dtm.getDayName(true);
189 * @param abbreviated Boolean When set to true the name will be abbreviated.
192 * @cat Plugins/Methods/Date
194 add("getDayName", function(abbreviated) {
195 return abbreviated ? Date.abbrDayNames[this.getDay()] : Date.dayNames[this.getDay()];
199 * Gets the name of the month.
201 * @example var dtm = new Date("01/12/2008");
202 * dtm.getMonthName();
205 * @example var dtm = new Date("01/12/2008");
206 * dtm.getMonthName(true);
209 * @param abbreviated Boolean When set to true the name will be abbreviated.
212 * @cat Plugins/Methods/Date
214 add("getMonthName", function(abbreviated) {
215 return abbreviated ? Date.abbrMonthNames[this.getMonth()] : Date.monthNames[this.getMonth()];
219 * Get the number of the day of the year.
221 * @example var dtm = new Date("01/12/2008");
222 * dtm.getDayOfYear();
227 * @cat Plugins/Methods/Date
229 add("getDayOfYear", function() {
230 var tmpdtm = new Date("1/1/" + this.getFullYear());
231 return Math.floor((this.getTime() - tmpdtm.getTime()) / 86400000);
235 * Get the number of the week of the year.
237 * @example var dtm = new Date("01/12/2008");
238 * dtm.getWeekOfYear();
241 * @name getWeekOfYear
243 * @cat Plugins/Methods/Date
245 add("getWeekOfYear", function() {
246 return Math.ceil(this.getDayOfYear() / 7);
250 * Set the day of the year.
252 * @example var dtm = new Date("01/12/2008");
253 * dtm.setDayOfYear(1);
255 * @result 'Tue Jan 01 2008 00:00:00'
259 * @cat Plugins/Methods/Date
261 add("setDayOfYear", function(day) {
268 * Add a number of years to the date object.
270 * @example var dtm = new Date("01/12/2008");
273 * @result 'Mon Jan 12 2009 00:00:00'
277 * @cat Plugins/Methods/Date
279 add("addYears", function(num) {
280 this.setFullYear(this.getFullYear() + num);
285 * Add a number of months to the date object.
287 * @example var dtm = new Date("01/12/2008");
290 * @result 'Tue Feb 12 2008 00:00:00'
294 * @cat Plugins/Methods/Date
296 add("addMonths", function(num) {
297 var tmpdtm = this.getDate();
299 this.setMonth(this.getMonth() + num);
301 if (tmpdtm > this.getDate())
302 this.addDays(-this.getDate());
308 * Add a number of days to the date object.
310 * @example var dtm = new Date("01/12/2008");
313 * @result 'Sun Jan 13 2008 00:00:00'
317 * @cat Plugins/Methods/Date
319 add("addDays", function(num) {
320 //this.setDate(this.getDate() + num);
321 this.setTime(this.getTime() + (num*86400000) );
326 * Add a number of hours to the date object.
328 * @example var dtm = new Date("01/12/2008");
331 * @result 'Sun Jan 13 2008 00:00:00'
335 * @cat Plugins/Methods/Date
337 add("addHours", function(num) {
338 this.setHours(this.getHours() + num);
343 * Add a number of minutes to the date object.
345 * @example var dtm = new Date("01/12/2008");
346 * dtm.addMinutes(60);
348 * @result 'Sat Jan 12 2008 01:00:00'
352 * @cat Plugins/Methods/Date
354 add("addMinutes", function(num) {
355 this.setMinutes(this.getMinutes() + num);
360 * Add a number of seconds to the date object.
362 * @example var dtm = new Date("01/12/2008");
363 * dtm.addSeconds(60);
365 * @result 'Sat Jan 12 2008 00:01:00'
369 * @cat Plugins/Methods/Date
371 add("addSeconds", function(num) {
372 this.setSeconds(this.getSeconds() + num);
377 * Sets the time component of this Date to zero for cleaner, easier comparison of dates where time is not relevant.
379 * @example var dtm = new Date();
382 * @result 'Sat Jan 12 2008 00:01:00'
386 * @cat Plugins/Methods/Date
387 * @author Kelvin Luck
389 add("zeroTime", function() {
390 this.setMilliseconds(0);
397 add("format", function(format) {
398 // MIT Licenced ! (don't you forget that !) by
400 // http://jacwright.com/projects/javascript/date_format/
401 if(this.getTime() == 0) return _("N/A");
403 var replace = Date.replaceChars;
404 for (var i = 0; i < format.length; i++) {
405 var curChar = format.charAt(i);
406 if (i - 1 >= 0 && format.charAt(i - 1) == "\\") {
407 returnStr += curChar;
409 else if (replace[curChar]) {
410 returnStr += replace[curChar].call(this);
411 } else if (curChar != "\\"){
412 returnStr += curChar;
418 Date.replaceChars = {
419 shortMonths: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
420 longMonths: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
421 shortDays: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
422 longDays: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
425 d: function() { return (this.getDate() < 10 ? '0' : '') + this.getDate(); },
426 D: function() { return Date.replaceChars.shortDays[this.getDay()]; },
427 j: function() { return this.getDate(); },
428 l: function() { return Date.replaceChars.longDays[this.getDay()]; },
429 N: function() { return this.getDay() + 1; },
430 S: function() { return (this.getDate() % 10 == 1 && this.getDate() != 11 ? 'st' : (this.getDate() % 10 == 2 && this.getDate() != 12 ? 'nd' : (this.getDate() % 10 == 3 && this.getDate() != 13 ? 'rd' : 'th'))); },
431 w: function() { return this.getDay(); },
432 z: function() { var d = new Date(this.getFullYear(),0,1); return Math.ceil((this - d) / 86400000); }, // Fixed now
434 W: function() { var d = new Date(this.getFullYear(), 0, 1); return Math.ceil((((this - d) / 86400000) + d.getDay() + 1) / 7); }, // Fixed now
436 F: function() { return Date.replaceChars.longMonths[this.getMonth()]; },
437 m: function() { return (this.getMonth() < 9 ? '0' : '') + (this.getMonth() + 1); },
438 M: function() { return Date.replaceChars.shortMonths[this.getMonth()]; },
439 n: function() { return this.getMonth() + 1; },
440 t: function() { var d = new Date(); return new Date(d.getFullYear(), d.getMonth(), 0).getDate() }, // Fixed now, gets #days of date
442 L: function() { var year = this.getFullYear(); return (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)); }, // Fixed now
443 o: function() { var d = new Date(this.valueOf()); d.setDate(d.getDate() - ((this.getDay() + 6) % 7) + 3); return d.getFullYear();}, //Fixed now
444 Y: function() { return this.getFullYear(); },
445 y: function() { return ('' + this.getFullYear()).substr(2); },
447 a: function() { return this.getHours() < 12 ? 'am' : 'pm'; },
448 A: function() { return this.getHours() < 12 ? 'AM' : 'PM'; },
449 B: function() { return Math.floor((((this.getUTCHours() + 1) % 24) + this.getUTCMinutes() / 60 + this.getUTCSeconds() / 3600) * 1000 / 24); }, // Fixed now
450 g: function() { return this.getHours() % 12 || 12; },
451 G: function() { return this.getHours(); },
452 h: function() { return ((this.getHours() % 12 || 12) < 10 ? '0' : '') + (this.getHours() % 12 || 12); },
453 H: function() { return (this.getHours() < 10 ? '0' : '') + this.getHours(); },
454 i: function() { return (this.getMinutes() < 10 ? '0' : '') + this.getMinutes(); },
455 s: function() { return (this.getSeconds() < 10 ? '0' : '') + this.getSeconds(); },
456 u: function() { var m = this.getMilliseconds(); return (m < 10 ? '00' : (m < 100 ? '0' : '')) + m; },
458 e: function() { return "Not Yet Supported"; },
461 for (var i = 0; i < 12; ++i) {
462 var d = new Date(this.getFullYear(), i, 1);
463 var offset = d.getTimezoneOffset();
465 if (DST === null) DST = offset;
466 else if (offset < DST) { DST = offset; break; } else if (offset > DST) break;
468 return (this.getTimezoneOffset() == DST) | 0;
470 O: function() { return (-this.getTimezoneOffset() < 0 ? '-' : '+') + (Math.abs(this.getTimezoneOffset() / 60) < 10 ? '0' : '') + (Math.abs(this.getTimezoneOffset() / 60)) + '00'; },
471 P: function() { return (-this.getTimezoneOffset() < 0 ? '-' : '+') + (Math.abs(this.getTimezoneOffset() / 60) < 10 ? '0' : '') + (Math.abs(this.getTimezoneOffset() / 60)) + ':00'; }, // Fixed now
472 T: function() { var m = this.getMonth(); this.setMonth(0); var result = this.toTimeString().replace(/^.+ \(?([^\)]+)\)?$/, '$1'); this.setMonth(m); return result;},
473 Z: function() { return -this.getTimezoneOffset() * 60; },
475 c: function() { return this.format("Y-m-d\\TH:i:sP"); }, // Fixed now
476 r: function() { return this.toString(); },
477 U: function() { return this.getTime() / 1000; }
482 * Returns a string representation of the date object according to Date.format.
483 * (Date.toString may be used in other places so I purposefully didn't overwrite it)
485 * @example var dtm = new Date("01/12/2008");
487 * @result '12/01/2008' // (where Date.format == 'dd/mm/yyyy'
491 * @cat Plugins/Methods/Date
492 * @author Kelvin Luck
494 add("asString", function(format) {
495 var r = format || Date.format;
497 .split('yyyy').join(this.getFullYear())
498 .split('yy').join((this.getFullYear() + '').substring(2))
499 .split('mmmm').join(this.getMonthName(false))
500 .split('mmm').join(this.getMonthName(true))
501 .split('mm').join(_zeroPad(this.getMonth()+1))
502 .split('dd').join(_zeroPad(this.getDate()))
503 .split('hh').join(_zeroPad(this.getHours()))
504 .split('min').join(_zeroPad(this.getMinutes()))
505 .split('ss').join(_zeroPad(this.getSeconds()));
509 * Returns a new date object created from the passed String according to Date.format or false if the attempt to do this results in an invalid date object
510 * (We can't simple use Date.parse as it's not aware of locale and I chose not to overwrite it incase it's functionality is being relied on elsewhere)
512 * @example var dtm = Date.fromString("12/01/2008");
514 * @result 'Sat Jan 12 2008 00:00:00' // (where Date.format == 'dd/mm/yyyy'
518 * @cat Plugins/Methods/Date
519 * @author Kelvin Luck
521 Date.fromString = function(s, format)
523 var f = format || Date.format;
524 var d = new Date('01/01/1977');
528 var iM = f.indexOf('mmmm');
530 for (var i=0; i<Date.monthNames.length; i++) {
531 var mStr = s.substr(iM, Date.monthNames[i].length);
532 if (Date.monthNames[i] == mStr) {
533 mLength = Date.monthNames[i].length - 4;
539 iM = f.indexOf('mmm');
541 var mStr = s.substr(iM, 3);
542 for (var i=0; i<Date.abbrMonthNames.length; i++) {
543 if (Date.abbrMonthNames[i] == mStr) break;
547 d.setMonth(Number(s.substr(f.indexOf('mm'), 2)) - 1);
551 var iY = f.indexOf('yyyy');
558 d.setFullYear(Number(s.substr(iY, 4)));
564 // TODO - this doesn't work very well - are there any rules for what is meant by a two digit year?
565 d.setFullYear(Number(Date.fullYearStart + s.substr(f.indexOf('yy'), 2)));
567 var iD = f.indexOf('dd');
572 d.setDate(Number(s.substr(iD, 2)));
573 if (isNaN(d.getTime())) {
580 var _zeroPad = function(num) {
582 return s.substring(s.length-2)
583 //return ('0'+num).substring(-2); // doesn't work on IE :(