├── dateFormat.min.js ├── README.md ├── Date.format.min.js ├── dateFormat.js └── Date.format.js /dateFormat.min.js: -------------------------------------------------------------------------------- 1 | ;function dateFormat(g,c,k){function l(a,b){a.setHours(a.getHours()+parseFloat(b));return a}function m(a,b){var c="Sunday Monday Tuesday Wednesday Thursday Friday Saturday".split(" ");return b?c[a.getDay()].substr(0,3):c[a.getDay()]}function n(a,b){var c="January February March April May June July August September October November December".split(" ");return b?c[a.getMonth()].substr(0,3):c[a.getMonth()]}var d={d:function(){var a=this.getDate();return 9=a?a:a-12},G:function(){return this.getHours()},h:function(){var a=this.getHours(),a=12>=a?a:a-12;return 0==a?12:9=a?a:a-12},G:function(){return this.getHours()},h:function(){var a=this.getHours(),a=12>=a?a:a-12;return 0==a?12:9 9 ? a : '0' + a; }, 12 | 'D': function() { return getDayName(this, true); }, 13 | 'j': function() { return this.getDate(); }, 14 | 'l': function() { return getDayName(this); }, 15 | 'N': function() { return this.getDay() + 1; }, 16 | 'S': function() { var a = this.getDate(); return /^1[0-9]$/.test(a) ? "th" : /1$/.test(a) ? "st" : /2$/.test(a) ? "nd" : /3$/.test(a) ? "rd" : "th"; }, 17 | 'w': function() { return this.getDay(); }, 18 | 'z': function() { return Math.round(Math.abs((this.getTime() - new Date('1/1/' + this.getFullYear()).getTime())/(8.64e7))); }, 19 | /* WEEK */ 20 | 'W': function() { return getWeek(this); }, 21 | /* MONTH */ 22 | 'F': function() { return getMonthName(this); }, 23 | 'm': function() { var a = this.getMonth() + 1; return a > 9 ? a : '0' + a; }, 24 | 'M': function() { return getMonthName(this, true); }, 25 | 'n': function() { return this.getMonth() + 1; }, 26 | 't': function() { return new Date(this.getFullYear(), this.getMonth()+1, 0).getDate() }, 27 | /* YEAR */ 28 | 'L': function() { var a = this.getFullYear(); return 0 == a % 4 && 0 != a % 100 || 0 == a % 400; }, 29 | 'o': function() { return parseInt(this.getFullYear()); }, // todo: base on week's parent year 30 | 'Y': function() { return parseInt(this.getFullYear()); }, 31 | 'y': function() { return parseInt((this.getFullYear()+'').substr(-2)); }, 32 | /* TIME */ 33 | 'a': function() { return this.getHours() >= 12 ? "pm" : "am"; }, 34 | 'A': function() { return this.getHours() >= 12 ? "PM" : "AM"; }, 35 | 'B': function() { return "@"+("00"+Math.floor((((this.getHours()+1)%24*60+this.getMinutes())*60+this.getSeconds()+(this.getMilliseconds()*0.001))/86.4)).slice(-3); }, 36 | 'g': function() { var a = this.getHours(); return a == 0 ? 12 : a <= 12 ? a : a - 12; }, // 12-hour format of an hour without leading zeros 37 | 'G': function() { return this.getHours(); }, // 24-hour format of an hour without leading zeros 38 | 'h': function() { var a = this.getHours(); a = a <= 12 ? a : a - 12; return a == 0 ? 12 : a > 9 ? a : '0' + a; }, // 12-hour format of an hour with leading zeros 39 | 'H': function() { var a = this.getHours(); return a > 9 ? a : '0' + a; }, // 24-hour format of an hour with leading zeros 40 | 'i': function() { var a = this.getMinutes(); return a > 9 ? a : '0' + a; }, // Minutes with leading zeros 41 | 's': function() { var a = this.getSeconds(); return a > 9 ? a : '0' + a; }, // Seconds, with leading zeros 42 | 'u': function() { return this.getMilliseconds(); }, // this is NOT microseconds ... it's JS :P, 43 | /* TIMEZONE */ 44 | 'e': function() { var a = this.toString().match(/ ([A-Z]{3,4})([-|+]?\d{4})/); return a.length > 1 ? a[1] : ''; }, 45 | 'I': function() { 46 | var a = new Date(this.getFullYear(), 0, 1), 47 | b = new Date(this.getFullYear(), 6, 1), 48 | c = Math.max(a.getTimezoneOffset(), b.getTimezoneOffset()); 49 | return this.getTimezoneOffset() < c ? 1 : 0; 50 | }, 51 | 'O': function() { var a = this.toString().match(/ ([A-Z]{3,4})([-|+]?\d{4})/); return a.length > 2 ? a[2] : ''; }, 52 | 'P': function() { var a = this.toString().match(/ ([A-Z]{3,4})([-|+]?\d{4})/); return a.length > 2 ? a[2].substr(0,3) + ':' + a[2].substr(3,2) : ''; }, 53 | 'T': function() { return this.toLocaleString('en', {timeZoneName:'short'}).split(' ').pop(); }, // may not be reliable on Apple Systems // NOTE: Apple Sux 54 | 'Z': function() { return this.getTimezoneOffset() * 60; }, 55 | /* FULL DATE/TIME */ 56 | 'c': function() { return addHours(new Date(this), -(this.getTimezoneOffset() / 60)).toISOString(); }, 57 | 'r': function() { return addHours(new Date(this), -(this.getTimezoneOffset() / 60)).toISOString(); }, 58 | 'U': function() { return this.getTime() / 1000 | 0; } 59 | }, 60 | compound = { 61 | 'commonLogFormat': 'd/M/Y:G:i:s', 62 | 'exif': 'Y:m:d G:i:s', 63 | 'isoYearWeek': 'Y\\WW', 64 | 'isoYearWeek2': 'Y-\\WW', 65 | 'isoYearWeekDay': 'Y\\WWj', 66 | 'isoYearWeekDay2': 'Y-\\WW-j', 67 | 'mySQL': 'Y-m-d h:i:s', 68 | 'postgreSQL': 'Y.z', 69 | 'postgreSQL2': 'Yz', 70 | 'soap': 'Y-m-d\\TH:i:s.u', 71 | 'soap2': 'Y-m-d\\TH:i:s.uP', 72 | 'unixTimestamp': '@U', 73 | 'xmlrpc': 'Ymd\\TG:i:s', 74 | 'xmlrpcCompact': 'Ymd\\tGis', 75 | 'wddx': 'Y-n-j\\TG:i:s' 76 | }, 77 | constants = { 78 | 'AMERICAN': 'F j, Y', 79 | 'AMERICANSHORT': 'm/d/Y', 80 | 'AMERICANSHORTWTIME': 'm/d/Y h:i:sA', 81 | 'ATOM': 'Y-m-d\\TH:i:sP', 82 | 'COOKIE': 'l, d-M-Y H:i:s T', 83 | 'EUROPEAN': 'j F Y', 84 | 'EUROPEANSHORT': 'd.m.Y', 85 | 'EUROPEANSHORTWTIME': 'd.m.Y H:i:s', 86 | 'ISO8601': 'Y-m-d\\TH:i:sO', 87 | 'LEGAL': 'j F Y', 88 | 'RFC822': 'D, d M y H:i:s O', 89 | 'RFC850': 'l, d-M-y H:i:s T', 90 | 'RFC1036': 'D, d M y H:i:s O', 91 | 'RFC1123': 'D, d M Y H:i:s O', 92 | 'RFC2822': 'D, d M Y H:i:s O', 93 | 'RFC3339': 'Y-m-d\\TH:i:sP', 94 | 'RSS': 'D, d M Y H:i:s O', 95 | 'W3C': 'Y-m-d\\TH:i:sP' 96 | }, 97 | pretty = { 98 | 'pretty-a': 'g:i.sA l jS \\o\\f F, Y', 99 | 'pretty-b': 'g:iA l jS \\o\\f F, Y', 100 | 'pretty-c': 'n/d/Y g:iA', 101 | 'pretty-d': 'n/d/Y', 102 | 'pretty-e': 'F jS - g:ia', 103 | 'pretty-f': 'g:iA', 104 | 'pretty-g': 'F jS, Y', 105 | 'pretty-h': 'F jS, Y g:mA' 106 | } 107 | if (str) { 108 | if (str == 'compound') { 109 | if (showFormatted === false) return compound; 110 | var r = {}; 111 | for (var x in compound) r[x] = dateFormat(dt, compound[x]); 112 | return r; 113 | } 114 | else if (compound[str]) return dateFormat(dt, compound[str], showFormatted); 115 | 116 | if (str == 'constants') { 117 | if (showFormatted === false) return constants; 118 | var r = {}; 119 | for (var x in constants) r[x] = dateFormat(dt, constants[x]); 120 | return r; 121 | } 122 | else if (constants[str]) return dateFormat(dt, constants[str], showFormatted); 123 | 124 | if (str == 'pretty') { 125 | if (showFormatted === false) return pretty; 126 | var r = {}; 127 | for (var x in pretty) r[x] = dateFormat(dt, pretty[x]); 128 | return r; 129 | } 130 | else if (pretty[str]) return dateFormat(dt, pretty[str], showFormatted); 131 | 132 | var ret = str.split(''), lc = ''; 133 | for (var x in ret) { 134 | var c = ret[x]; 135 | if ((c && /[a-z]/i.test(c)) && !(/\\/.test(lc + c))) { 136 | var rx = new RegExp(c, 'g'); 137 | ret[x] = formats[c] ? formats[c].apply(dt) : c; 138 | } 139 | lc = ret[x]; 140 | } 141 | return ret.join('').replace(/\\/g, ''); 142 | } 143 | return dt; 144 | } 145 | -------------------------------------------------------------------------------- /Date.format.js: -------------------------------------------------------------------------------- 1 | ;(function() { // Date.format 2 | function addHours(d, v) { d.setHours(d.getHours() + parseFloat(v)); return d; } 3 | function getDayName(d, sf) { var days = [ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" ]; return sf ? days[d.getDay()].substr(0,3) : days[d.getDay()]; } 4 | function getWeek(d) { var a = new Date(d.getFullYear(), 0, 1); return Math.ceil(((d - a) / 864E5 + a.getDay() + 1) / 7); } 5 | function getMonthName(d, shortForm) { 6 | var months = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ]; 7 | return shortForm ? months[d.getMonth()].substr(0,3) : months[d.getMonth()]; 8 | } 9 | var formats = { 10 | /* DAY */ 11 | 'd': function() { var a = this.getDate(); return a > 9 ? a : '0' + a; }, 12 | 'D': function() { return getDayName(this, true); }, 13 | 'j': function() { return this.getDate(); }, 14 | 'l': function() { return getDayName(this); }, 15 | 'N': function() { return this.getDay() + 1; }, 16 | 'S': function() { var a = this.getDate(); return /^1[0-9]$/.test(a) ? "th" : /1$/.test(a) ? "st" : /2$/.test(a) ? "nd" : /3$/.test(a) ? "rd" : "th"; }, 17 | 'w': function() { return this.getDay(); }, 18 | 'z': function() { return Math.round(Math.abs((this.getTime() - new Date('1/1/' + this.getFullYear()).getTime())/(8.64e7))); }, 19 | /* WEEK */ 20 | 'W': function() { return getWeek(this); }, 21 | /* MONTH */ 22 | 'F': function() { return getMonthName(this); }, 23 | 'm': function() { var a = this.getMonth() + 1; return a > 9 ? a : '0' + a; }, 24 | 'M': function() { return getMonthName(this, true); }, 25 | 'n': function() { return this.getMonth() + 1; }, 26 | 't': function() { return new Date(this.getFullYear(), this.getMonth()+1, 0).getDate() }, 27 | /* YEAR */ 28 | 'L': function() { var a = this.getFullYear(); return 0 == a % 4 && 0 != a % 100 || 0 == a % 400; }, 29 | 'o': function() { return parseInt(this.getFullYear()); }, // todo: base on week's parent year 30 | 'Y': function() { return parseInt(this.getFullYear()); }, 31 | 'y': function() { return parseInt((this.getFullYear()+'').substr(-2)); }, 32 | /* TIME */ 33 | 'a': function() { return this.getHours() >= 12 ? "pm" : "am"; }, 34 | 'A': function() { return this.getHours() >= 12 ? "PM" : "AM"; }, 35 | 'B': function() { return "@"+("00"+Math.floor((((this.getHours()+1)%24*60+this.getMinutes())*60+this.getSeconds()+(this.getMilliseconds()*0.001))/86.4)).slice(-3); }, 36 | 'g': function() { var a = this.getHours(); return a == 0 ? 12 : a <= 12 ? a : a - 12; }, // 12-hour format of an hour without leading zeros 37 | 'G': function() { return this.getHours(); }, // 24-hour format of an hour without leading zeros 38 | 'h': function() { var a = this.getHours(); a = a <= 12 ? a : a - 12; return a == 0 ? 12 : a > 9 ? a : '0' + a; }, // 12-hour format of an hour with leading zeros 39 | 'H': function() { var a = this.getHours(); return a > 9 ? a : '0' + a; }, // 24-hour format of an hour with leading zeros 40 | 'i': function() { var a = this.getMinutes(); return a > 9 ? a : '0' + a; }, // Minutes with leading zeros 41 | 's': function() { var a = this.getSeconds(); return a > 9 ? a : '0' + a; }, // Seconds, with leading zeros 42 | 'u': function() { return this.getMilliseconds(); }, // this is NOT microseconds ... it's JS :P, 43 | /* TIMEZONE */ 44 | 'e': function() { var a = this.toString().match(/ ([A-Z]{3,4})([-|+]?\d{4})/); return a.length > 1 ? a[1] : ''; }, 45 | 'I': function() { 46 | var a = new Date(this.getFullYear(), 0, 1), 47 | b = new Date(this.getFullYear(), 6, 1), 48 | c = Math.max(a.getTimezoneOffset(), b.getTimezoneOffset()); 49 | return this.getTimezoneOffset() < c ? 1 : 0; 50 | }, 51 | 'O': function() { var a = this.toString().match(/ ([A-Z]{3,4})([-|+]?\d{4})/); return a.length > 2 ? a[2] : ''; }, 52 | 'P': function() { var a = this.toString().match(/ ([A-Z]{3,4})([-|+]?\d{4})/); return a.length > 2 ? a[2].substr(0,3) + ':' + a[2].substr(3,2) : ''; }, 53 | 'T': function() { return this.toLocaleString('en', {timeZoneName:'short'}).split(' ').pop(); }, // may not be reliable on Apple Systems // NOTE: Apple Sux 54 | 'Z': function() { return this.getTimezoneOffset() * 60; }, 55 | /* FULL DATE/TIME */ 56 | 'c': function() { return addHours(new Date(this), -(this.getTimezoneOffset() / 60)).toISOString(); }, 57 | 'r': function() { return addHours(new Date(this), -(this.getTimezoneOffset() / 60)).toISOString(); }, 58 | 'U': function() { return this.getTime() / 1000 | 0; } 59 | }, 60 | compound = { 61 | 'commonLogFormat': 'd/M/Y:G:i:s', 62 | 'exif': 'Y:m:d G:i:s', 63 | 'isoYearWeek': 'Y\\WW', 64 | 'isoYearWeek2': 'Y-\\WW', 65 | 'isoYearWeekDay': 'Y\\WWj', 66 | 'isoYearWeekDay2': 'Y-\\WW-j', 67 | 'mySQL': 'Y-m-d h:i:s', 68 | 'postgreSQL': 'Y.z', 69 | 'postgreSQL2': 'Yz', 70 | 'soap': 'Y-m-d\\TH:i:s.u', 71 | 'soap2': 'Y-m-d\\TH:i:s.uP', 72 | 'unixTimestamp': '@U', 73 | 'xmlrpc': 'Ymd\\TG:i:s', 74 | 'xmlrpcCompact': 'Ymd\\tGis', 75 | 'wddx': 'Y-n-j\\TG:i:s' 76 | }, 77 | constants = { 78 | 'AMERICAN': 'F j, Y', 79 | 'AMERICANSHORT': 'm/d/Y', 80 | 'AMERICANSHORTWTIME': 'm/d/Y h:i:sA', 81 | 'ATOM': 'Y-m-d\\TH:i:sP', 82 | 'COOKIE': 'l, d-M-Y H:i:s T', 83 | 'EUROPEAN': 'j F Y', 84 | 'EUROPEANSHORT': 'd.m.Y', 85 | 'EUROPEANSHORTWTIME': 'd.m.Y H:i:s', 86 | 'ISO8601': 'Y-m-d\\TH:i:sO', 87 | 'LEGAL': 'j F Y', 88 | 'RFC822': 'D, d M y H:i:s O', 89 | 'RFC850': 'l, d-M-y H:i:s T', 90 | 'RFC1036': 'D, d M y H:i:s O', 91 | 'RFC1123': 'D, d M Y H:i:s O', 92 | 'RFC2822': 'D, d M Y H:i:s O', 93 | 'RFC3339': 'Y-m-d\\TH:i:sP', 94 | 'RSS': 'D, d M Y H:i:s O', 95 | 'W3C': 'Y-m-d\\TH:i:sP' 96 | }, 97 | pretty = { 98 | 'pretty-a': 'g:i.sA l jS \\o\\f F, Y', 99 | 'pretty-b': 'g:iA l jS \\o\\f F, Y', 100 | 'pretty-c': 'n/d/Y g:iA', 101 | 'pretty-d': 'n/d/Y', 102 | 'pretty-e': 'F jS - g:ia', 103 | 'pretty-f': 'g:iA', 104 | 'pretty-g': 'F jS, Y', 105 | 'pretty-h': 'F jS, Y g:mA' 106 | } 107 | 108 | // utc param currently does nothing except bool false to return list of constants as pure strings (no format) 109 | function dateFormat(str, utc) { // string, boolean TODO: use utc bool to determine in converter methods if to use UTC version 110 | if (str) { 111 | if (str == 'compound') { 112 | if (utc === false) return this.format.compound; 113 | var r = {}; 114 | for (var x in Date.prototype.format.compound) r[x] = this.format(Date.prototype.format.compound[x]); 115 | return r; 116 | } 117 | else if (Date.prototype.format.compound[str]) return this.format(Date.prototype.format.compound[str], utc); 118 | 119 | if (str == 'constants') { 120 | if (utc === false) return this.format.constants; 121 | var r = {}; 122 | for (var x in Date.prototype.format.constants) r[x] = this.format(Date.prototype.format.constants[x]); 123 | return r; 124 | } 125 | else if (Date.prototype.format.constants[str]) return this.format(Date.prototype.format.constants[str], utc); 126 | 127 | if (str == 'pretty') { 128 | if (utc === false) return this.format.pretty; 129 | var r = {}; 130 | for (var x in Date.prototype.format.pretty) r[x] = this.format(Date.prototype.format.pretty[x]); 131 | return r; 132 | } 133 | else if (Date.prototype.format.pretty[str]) return this.format(Date.prototype.format.pretty[str], utc); 134 | 135 | var ret = str.split(''), lc = ''; 136 | for (var x in ret) { 137 | var c = ret[x]; 138 | if ((c && /[a-z]/i.test(c)) && !(/\\/.test(lc + c))) { 139 | var rx = new RegExp(c, 'g'); 140 | ret[x] = formats[c] ? formats[c].apply(this) : c; 141 | } 142 | lc = ret[x]; 143 | } 144 | return ret.join('').replace(/\\/g, ''); 145 | } 146 | return str; 147 | } 148 | 149 | Object['defineProperty'] ? Object.defineProperty(dateFormat, 'compound', { value: compound }) : dateFormat['compound'] = compound; 150 | Object['defineProperty'] ? Object.defineProperty(dateFormat, 'constants', { value: constants }) : dateFormat['constants'] = constants; 151 | Object['defineProperty'] ? Object.defineProperty(dateFormat, 'pretty', { value: pretty }) : dateFormat['pretty'] = pretty; 152 | 153 | Object['defineProperty'] && !Date.prototype.hasOwnProperty('format') ? Object.defineProperty(Date.prototype, 'format', { value: dateFormat }) : Date.prototype['format'] = dateFormat; 154 | })(); 155 | --------------------------------------------------------------------------------