/* * $Id: dateextensions.js 43 2006-08-17 19:11:44Z wingedfox $ * $HeadURL: https://svn.debugger.ru/repos/jslibs/BrowserExtensions/tags/BrowserExtensions.003/dateextensions.js $ * * Extension implements additional methods to operate with Date object * @author Ilya Lebedev * @modified $Date: 2006-08-17 23:11:44 +0400 (Чтв, 17 Авг 2006) $ * @version $Rev: 43 $ * @license LGPL 2.1 or later */ var MONTH_NAMES=new Array('January','February','March','April','May','June','July','August','September','October','November','December','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'); var DAY_NAMES=new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sun','Mon','Tue','Wed','Thu','Fri','Sat'); /* * leap year check * * @return bool check result * @access public */ Date.prototype.isLeapYear = function () { var y = this.getFullYear(); return (y % 4 == 0 && y % 100 != 0) || y % 400 == 0; } /* * the day of year number * * @return int day of year * @access public */ Date.prototype.getDayOfYear = function () { var md = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334], m = this.getMonth(), d = this.getDate()+md[m]; return this.isLeapYear()&&m>2?d+1:d; } /* * return year according to Iso notation * * @return int year number * @access public */ Date.prototype.getIsoYear = function () { var d = this.getDayOfYear(), j1 = (new Date(this.getFullYear(),0,1)).getIsoDay(), y = this.getFullYear(); if (d <= (8 - j1) && j1 > 4) { return y-1; } else if (((this.isLeapYear()?366:365) - d) < (4 - this.getIsoDay())) { return y+1; } else { return y; } } /* * find day number in ISO notation (Mon = 1, Sun=7) * * @return day number * @access public */ Date.prototype.getIsoDay = function () { var y = this.getFullYear(), yy = (y-1) % 100, c = (y-1) - yy, g = yy + Math.floor(yy/4), j1 = 1 + ((((Math.floor(c / 100) % 4) * 5) + g) % 7); return (1 + ((this.getDayOfYear() + (j1 - 1) - 1) % 7)); } /* * return week number in ISO notation * * @return int week number * @access public */ Date.prototype.getIsoWeek = function () { var y = this.getFullYear(), yi = this.getIsoYear(), j1 = (new Date(y,0,1)).getIsoDay(); if ( yi < y ) { if (j1 == 5 || (j1 == 6 && (new Date(yi,0,1)).isLeapYear())) { w = 53; } else { w = 52; } } else if (yi > y) { w = 1; } else { var w = Math.floor((this.getDayOfYear() + (7-this.getIsoDay()) + (j1 - 1)) / 7); if (j1 > 4) w -= 1; } return w; } /* * Converts Date object to formatted string * * @description * Possible formatting options * %a - abbreviated weekday name according to the current locale * %A - full weekday name according to the current locale * %b - abbreviated month name according to the current locale * %B - full month name according to the current locale * %c - preferred date and time representation for the current locale * %C - century number (the year divided by 100 and truncated to an integer, range 00 to 99) * %d - day of the month as a decimal number (range 01 to 31) * %D - same as %m/%d/%y * %e - day of the month as a decimal number, a single digit is preceded by a space (range ' 1' to '31') * %g - like %G, but without the century. * %G - The 4-digit year corresponding to the ISO week number (see %V). * self has the same format and value as %Y, except that if the ISO week number belongs * to the previous or next year, that year is used instead. * %h - same as %b * %H - hour as a decimal number using a 24-hour clock (range 00 to 23) * %I - hour as a decimal number using a 12-hour clock (range 01 to 12) * %j - day of the year as a decimal number (range 001 to 366) * %m - month as a decimal number (range 01 to 12) * %M - minute as a decimal number * %n - newline character * %p - either `am' or `pm' according to the given time value, or the corresponding strings for the current locale * %r - time in a.m. and p.m. notation * %R - time in 24 hour notation * %S - second as a decimal number * %t - tab character * %T - current time, equal to %H:%M:%S * %V - The ISO 8601:1988 week number of the current year as a decimal number, range 01 to 53, * where week 1 is the first week that has at least 4 days in the current year, * and with Monday as the first day of the week. * (Use %G or %g for the year component that corresponds to the week number for the specified timestamp.) * %u - weekday as a decimal number [1,7], with 1 representing Monday * %U - week number of the current year as a decimal number, starting with the first Sunday as the first day of the first week * %w - day of the week as a decimal, Sunday being 0 * %W - week number of the current year as a decimal number, starting with the first Monday as the first day of the first week * %x - preferred date representation for the current locale without the time * %X - preferred time representation for the current locale without the date * %y - year as a decimal number without a century (range 00 to 99) * %Y - year as a decimal number including the century * %Z or %z - time zone or name or abbreviation * * @link http://php.net/strftime * * @param string optional date format * @param string optional single char spacer, used to pad short values * @return string formatted Date * @access public */ Date.prototype.toFormatString = function (fmt, spacer) { var self = this; if (!fmt) return this.toString(); if (typeof spacer != 'string') spacer = "0"; if (spacer.length > 1) spacer.length = 1; return fmt.replace(/%\w+/g,function(a) { a = a.replace(/[%\s]/,""); switch (a) { case "a" : return DAY_NAMES[self.getDay()]; case "A" : return DAY_NAMES[self.getDay()+7]; case "b" : case "h" : return MONTH_NAMES[self.getMonth()]; case "B" : return MONTH_NAMES[self.getMonth()+12]; case "c" : return; //??? case "C" : return Math.round(self.getFullYear()/100); case "d" : return String(self.getDate()).padLeft(2,spacer); case "D" : return self.toFormatString("%m/%d/%y", spacer);//String(self.getMonth()+1).padLeft(2,"0")+"/"+String(self.getDate()+1).padLeft(2,"0")+"/"+String(self.getFullYear()).slice(-2); case "e" : return String(self.getDate()+1).padLeft(2); case "g" : return self.getIsoYear().slice(-2); case "G" : return self.getIsoYear(); case "H" : return String(self.getHours()).padLeft(2,spacer); case "I" : return (self.getHours()>12?self.getHours()-12:self.getHours()).padLeft(2,spacer); case "j" : return String(self.getDayOfYear()).padLeft(3,spacer); case "m" : return String(self.getMonth()+1).padLeft(2,spacer); case "M" : return String(self.getMinutes()).padLeft(2,spacer); case "n" : return "\n"; case "p" : return (self.getHours()>12?"PM":"AM"); case "r" : return self.toFormatString("%I", spacer)+":"+self.toFormatString("%M", spacer)+":"+self.toFormatString("%S", spacer)+" "+self.toFormatString("%p", spacer); case "R" : return self.toFormatString("%H", spacer)+":"+self.toFormatString("%M", spacer); case "S" : return String(self.getSeconds()).padLeft(2,spacer); case "t" : return "\t"; case "T" : return self.toFormatString("%H", spacer)+":"+self.toFormatString("%M", spacer)+":"+self.toFormatString("%S", spacer); case "u" : return self.getIsoDay(); case "U" : return String(parseInt((self.getDayOfYear() - 1 - self.getIsoDay() + 13) / 7 - 1)).padLeft(2,"0"); case "V" : return String(self.getIsoWeek()).padLeft(2,spacer); case "w" : return self.getDay(); case "W" : return String(parseInt((self.getDayOfYear() - 1 - self.getDay() + 13) / 7 - 1)).padLeft(2,"0"); case "x" : return; // ??? case "X" : return; // ??? case "y" : return String(self.getFullYear()).slice(-2); case "Y" : return self.getFullYear(); case "z" : return; // ??? case "Z" : return self.getTimezoneOffset() / 60; } return a; } ) }