michael@0: function arrayExists(array, x) { michael@0: /* BEGIN LOOP */ michael@0: for (var i = 0; i < array.length; i++) { michael@0: if (array[i] == x) return true; michael@0: } michael@0: /* END LOOP */ michael@0: return false; michael@0: } michael@0: michael@0: Date.prototype.formatDate = function (input,time) { michael@0: // formatDate : michael@0: // a PHP date like function, for formatting date strings michael@0: // See: http://www.php.net/date michael@0: // michael@0: // input : format string michael@0: // time : epoch time (seconds, and optional) michael@0: // michael@0: // if time is not passed, formatting is based on michael@0: // the current "this" date object's set time. michael@0: // michael@0: // supported: michael@0: // a, A, B, d, D, F, g, G, h, H, i, j, l (lowercase L), L, michael@0: // m, M, n, O, r, s, S, t, U, w, W, y, Y, z michael@0: // michael@0: // unsupported: michael@0: // I (capital i), T, Z michael@0: michael@0: var switches = ["a", "A", "B", "d", "D", "F", "g", "G", "h", "H", michael@0: "i", "j", "l", "L", "m", "M", "n", "O", "r", "s", michael@0: "S", "t", "U", "w", "W", "y", "Y", "z"]; michael@0: var daysLong = ["Sunday", "Monday", "Tuesday", "Wednesday", michael@0: "Thursday", "Friday", "Saturday"]; michael@0: var daysShort = ["Sun", "Mon", "Tue", "Wed", michael@0: "Thu", "Fri", "Sat"]; michael@0: var monthsShort = ["Jan", "Feb", "Mar", "Apr", michael@0: "May", "Jun", "Jul", "Aug", "Sep", michael@0: "Oct", "Nov", "Dec"]; michael@0: var monthsLong = ["January", "February", "March", "April", michael@0: "May", "June", "July", "August", "September", michael@0: "October", "November", "December"]; michael@0: var daysSuffix = ["st", "nd", "rd", "th", "th", "th", "th", // 1st - 7th michael@0: "th", "th", "th", "th", "th", "th", "th", // 8th - 14th michael@0: "th", "th", "th", "th", "th", "th", "st", // 15th - 21st michael@0: "nd", "rd", "th", "th", "th", "th", "th", // 22nd - 28th michael@0: "th", "th", "st"]; // 29th - 31st michael@0: michael@0: function a() { michael@0: // Lowercase Ante meridiem and Post meridiem michael@0: return self.getHours() > 11? "pm" : "am"; michael@0: } michael@0: function A() { michael@0: // Uppercase Ante meridiem and Post meridiem michael@0: return self.getHours() > 11? "PM" : "AM"; michael@0: } michael@0: michael@0: function B(){ michael@0: // Swatch internet time. code simply grabbed from ppk, michael@0: // since I was feeling lazy: michael@0: // http://www.xs4all.nl/~ppk/js/beat.html michael@0: var off = (self.getTimezoneOffset() + 60)*60; michael@0: var theSeconds = (self.getHours() * 3600) + michael@0: (self.getMinutes() * 60) + michael@0: self.getSeconds() + off; michael@0: var beat = Math.floor(theSeconds/86.4); michael@0: if (beat > 1000) beat -= 1000; michael@0: if (beat < 0) beat += 1000; michael@0: if ((""+beat).length == 1) beat = "00"+beat; michael@0: if ((""+beat).length == 2) beat = "0"+beat; michael@0: return beat; michael@0: } michael@0: michael@0: function d() { michael@0: // Day of the month, 2 digits with leading zeros michael@0: return new String(self.getDate()).length == 1? michael@0: "0"+self.getDate() : self.getDate(); michael@0: } michael@0: function D() { michael@0: // A textual representation of a day, three letters michael@0: return daysShort[self.getDay()]; michael@0: } michael@0: function F() { michael@0: // A full textual representation of a month michael@0: return monthsLong[self.getMonth()]; michael@0: } michael@0: function g() { michael@0: // 12-hour format of an hour without leading zeros michael@0: return self.getHours() > 12? self.getHours()-12 : self.getHours(); michael@0: } michael@0: function G() { michael@0: // 24-hour format of an hour without leading zeros michael@0: return self.getHours(); michael@0: } michael@0: function h() { michael@0: // 12-hour format of an hour with leading zeros michael@0: if (self.getHours() > 12) { michael@0: var s = new String(self.getHours()-12); michael@0: return s.length == 1? michael@0: "0"+ (self.getHours()-12) : self.getHours()-12; michael@0: } else { michael@0: var s = new String(self.getHours()); michael@0: return s.length == 1? michael@0: "0"+self.getHours() : self.getHours(); michael@0: } michael@0: } michael@0: function H() { michael@0: // 24-hour format of an hour with leading zeros michael@0: return new String(self.getHours()).length == 1? michael@0: "0"+self.getHours() : self.getHours(); michael@0: } michael@0: function i() { michael@0: // Minutes with leading zeros michael@0: return new String(self.getMinutes()).length == 1? michael@0: "0"+self.getMinutes() : self.getMinutes(); michael@0: } michael@0: function j() { michael@0: // Day of the month without leading zeros michael@0: return self.getDate(); michael@0: } michael@0: function l() { michael@0: // A full textual representation of the day of the week michael@0: return daysLong[self.getDay()]; michael@0: } michael@0: function L() { michael@0: // leap year or not. 1 if leap year, 0 if not. michael@0: // the logic should match iso's 8601 standard. michael@0: var y_ = Y(); michael@0: if ( michael@0: (y_ % 4 == 0 && y_ % 100 != 0) || michael@0: (y_ % 4 == 0 && y_ % 100 == 0 && y_ % 400 == 0) michael@0: ) { michael@0: return 1; michael@0: } else { michael@0: return 0; michael@0: } michael@0: } michael@0: function m() { michael@0: // Numeric representation of a month, with leading zeros michael@0: return self.getMonth() < 9? michael@0: "0"+(self.getMonth()+1) : michael@0: self.getMonth()+1; michael@0: } michael@0: function M() { michael@0: // A short textual representation of a month, three letters michael@0: return monthsShort[self.getMonth()]; michael@0: } michael@0: function n() { michael@0: // Numeric representation of a month, without leading zeros michael@0: return self.getMonth()+1; michael@0: } michael@0: function O() { michael@0: // Difference to Greenwich time (GMT) in hours michael@0: var os = Math.abs(self.getTimezoneOffset()); michael@0: var h = ""+Math.floor(os/60); michael@0: var m = ""+(os%60); michael@0: h.length == 1? h = "0"+h:1; michael@0: m.length == 1? m = "0"+m:1; michael@0: return self.getTimezoneOffset() < 0 ? "+"+h+m : "-"+h+m; michael@0: } michael@0: function r() { michael@0: // RFC 822 formatted date michael@0: var r; // result michael@0: // Thu , 21 Dec 2000 michael@0: r = D() + ", " + j() + " " + M() + " " + Y() + michael@0: // 16 : 01 : 07 +0200 michael@0: " " + H() + ":" + i() + ":" + s() + " " + O(); michael@0: return r; michael@0: } michael@0: function S() { michael@0: // English ordinal suffix for the day of the month, 2 characters michael@0: return daysSuffix[self.getDate()-1]; michael@0: } michael@0: function s() { michael@0: // Seconds, with leading zeros michael@0: return new String(self.getSeconds()).length == 1? michael@0: "0"+self.getSeconds() : self.getSeconds(); michael@0: } michael@0: function t() { michael@0: michael@0: // thanks to Matt Bannon for some much needed code-fixes here! michael@0: var daysinmonths = [null,31,28,31,30,31,30,31,31,30,31,30,31]; michael@0: if (L()==1 && n()==2) return 29; // leap day michael@0: return daysinmonths[n()]; michael@0: } michael@0: function U() { michael@0: // Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT) michael@0: return Math.round(self.getTime()/1000); michael@0: } michael@0: function W() { michael@0: // Weeknumber, as per ISO specification: michael@0: // http://www.cl.cam.ac.uk/~mgk25/iso-time.html michael@0: michael@0: // if the day is three days before newyears eve, michael@0: // there's a chance it's "week 1" of next year. michael@0: // here we check for that. michael@0: var beforeNY = 364+L() - z(); michael@0: var afterNY = z(); michael@0: var weekday = w()!=0?w()-1:6; // makes sunday (0), into 6. michael@0: if (beforeNY <= 2 && weekday <= 2-beforeNY) { michael@0: return 1; michael@0: } michael@0: // similarly, if the day is within threedays of newyears michael@0: // there's a chance it belongs in the old year. michael@0: var ny = new Date("January 1 " + Y() + " 00:00:00"); michael@0: var nyDay = ny.getDay()!=0?ny.getDay()-1:6; michael@0: if ( michael@0: (afterNY <= 2) && michael@0: (nyDay >=4) && michael@0: (afterNY >= (6-nyDay)) michael@0: ) { michael@0: // Since I'm not sure we can just always return 53, michael@0: // i call the function here again, using the last day michael@0: // of the previous year, as the date, and then just michael@0: // return that week. michael@0: var prevNY = new Date("December 31 " + (Y()-1) + " 00:00:00"); michael@0: return prevNY.formatDate("W"); michael@0: } michael@0: michael@0: // week 1, is the week that has the first thursday in it. michael@0: // note that this value is not zero index. michael@0: if (nyDay <= 3) { michael@0: // first day of the year fell on a thursday, or earlier. michael@0: return 1 + Math.floor( ( z() + nyDay ) / 7 ); michael@0: } else { michael@0: // first day of the year fell on a friday, or later. michael@0: return 1 + Math.floor( ( z() - ( 7 - nyDay ) ) / 7 ); michael@0: } michael@0: } michael@0: function w() { michael@0: // Numeric representation of the day of the week michael@0: return self.getDay(); michael@0: } michael@0: michael@0: function Y() { michael@0: // A full numeric representation of a year, 4 digits michael@0: michael@0: // we first check, if getFullYear is supported. if it michael@0: // is, we just use that. ppks code is nice, but wont michael@0: // work with dates outside 1900-2038, or something like that michael@0: if (self.getFullYear) { michael@0: var newDate = new Date("January 1 2001 00:00:00 +0000"); michael@0: var x = newDate .getFullYear(); michael@0: if (x == 2001) { michael@0: // i trust the method now michael@0: return self.getFullYear(); michael@0: } michael@0: } michael@0: // else, do this: michael@0: // codes thanks to ppk: michael@0: // http://www.xs4all.nl/~ppk/js/introdate.html michael@0: var x = self.getYear(); michael@0: var y = x % 100; michael@0: y += (y < 38) ? 2000 : 1900; michael@0: return y; michael@0: } michael@0: function y() { michael@0: // A two-digit representation of a year michael@0: var y = Y()+""; michael@0: return y.substring(y.length-2,y.length); michael@0: } michael@0: function z() { michael@0: // The day of the year, zero indexed! 0 through 366 michael@0: var t = new Date("January 1 " + Y() + " 00:00:00"); michael@0: var diff = self.getTime() - t.getTime(); michael@0: return Math.floor(diff/1000/60/60/24); michael@0: } michael@0: michael@0: var self = this; michael@0: if (time) { michael@0: // save time michael@0: var prevTime = self.getTime(); michael@0: self.setTime(time); michael@0: } michael@0: michael@0: var ia = input.split(""); michael@0: var ij = 0; michael@0: /* BEGIN LOOP */ michael@0: while (ia[ij]) { michael@0: if (ia[ij] == "\\") { michael@0: // this is our way of allowing users to escape stuff michael@0: ia.splice(ij,1); michael@0: } else { michael@0: if (arrayExists(switches,ia[ij])) { michael@0: ia[ij] = eval(ia[ij] + "()"); michael@0: } michael@0: } michael@0: ij++; michael@0: } michael@0: /* END LOOP */ michael@0: // reset time, back to what it was michael@0: if (prevTime) { michael@0: self.setTime(prevTime); michael@0: } michael@0: return ia.join(""); michael@0: } michael@0: michael@0: var date = new Date("1/1/2007 1:11:11"); michael@0: michael@0: /* BEGIN LOOP */ michael@0: for (i = 0; i < 500; ++i) { michael@0: var shortFormat = date.formatDate("Y-m-d"); michael@0: var longFormat = date.formatDate("l, F d, Y g:i:s A"); michael@0: date.setTime(date.getTime() + 84266956); michael@0: } michael@0: /* END LOOP */ michael@0: