/** * @copyFrom http://Finn-Rasmussen.com/myphp-1.10/myphp-1.10-js/html/page/DateFormat.js.php * Copyrigt notice: You may copy the source code as is as long as you keep the copy right notice intact * * @package javascript * @author http://Finn-Rasmussen.com * @copyright http://Finn-Rasmussen.com * @version 1.10 * @since 22-feb-2007 */ var DateFormat = new function() { this.DASH = '-'; this.SLASH = '/'; this.LONG = 'dd-mm-yyyy'; // Long style pattern this.SHORT = 'ddmmyy'; // Short style pattern this.styleDate = "date"; // TODO this.locale = Locale.getInstance(); this.getInstance = function(locale) { if (typeof locale !== 'undefined') { this.locale = locale; } else { alert(typeof locale); } return this; } this.getDateInstance = function(styleDate, locale) { this.styleDate = styleDate; return this.getInstance(locale); } this.format = function(strDate) { var pattern = this.adjust(strDate); var date = this.newDate(pattern); var format = this.parse(date); return format; } this.adjust = function(strDate) { var pattern = strDate; var today = new Date(); if (pattern.length === 1 || pattern.length === 2) { // Expected strDate d(1) or dd(2) var date = this.getDate(pattern); var now = new Date(today.getFullYear(), today.getMonth(), date); pattern = '' + now.getDate() + this.SLASH + (now.getMonth() + 1) + this.SLASH + now.getFullYear(); } if (pattern.length === 3 || pattern.length === 4) { if (pattern.indexOf(this.DASH) === -1 && pattern.indexOf(this.SLASH) === -1) { // Expected strDate dmm(3) or ddmm(4) var pos = pattern.length===3?1:2; // 0123 var date = parseInt(pattern.substring(0,pos),10); var month = parseInt(pattern.substring(pos),10); var now = new Date(today.getFullYear(), month-1, date); pattern = '' + now.getDate() + this.SLASH + (now.getMonth() + 1) + this.SLASH + now.getFullYear(); } else { // Expected strDate d-m(3), d-mm(4) var date = this.getDate(pattern); var month = this.getMonth(pattern); var now = new Date(today.getFullYear(), month-1, date); pattern = '' + now.getDate() + this.SLASH + (now.getMonth() + 1) + this.SLASH + now.getFullYear(); } } if (pattern.length === 5 || pattern.length === 7) { if (pattern.indexOf(this.DASH) === -1 && pattern.indexOf(this.SLASH) === -1) { // Expected strdate dmmyy(5), dmmyyyy(7) pattern = '0' + pattern; } else { // Expected strdate dd-mm(5), d-mm-yy(7), dd-m-yy(7) var date = this.getDate(pattern); var month = this.getMonth(pattern); var year = this.getYear(pattern); var now = new Date(year, month-1, date); pattern = '' + now.getDate() + this.SLASH + (now.getMonth() + 1) + this.SLASH + now.getFullYear(); } } return pattern; } this.newDate = function(pattern) { var date = this.getDate(pattern); var month = this.getMonth(pattern); var year = this.getYear(pattern); return new Date(year, month-1, date); } this.getDate = function(pattern) { var start = this.getStart(pattern,0); var end = this.getEnd(pattern,1); var date = parseInt(pattern.substring(start, end),10); if (isNaN(date)) { var today = new Date(); date = today.getDate(); } return date; } this.getMonth = function(pattern) { var start = this.getStart(pattern,2); var end = this.getEnd(pattern,3); var month = parseInt(pattern.substring(start, end),10); if (isNaN(month)) { var today = new Date(); month = today.getMonth() + 1; } return month; } this.getYear = function(pattern) { var start = this.getStart(pattern,4); var end = this.getEnd(pattern,5); var year = parseInt(pattern.substring(start, end),10); if (isNaN(year)) { var today = new Date(); year = today.getFullYear(); } if (year < 100) { year = 2000 + year; } return year; } this.getStart = function(pattern,level) { var pos = this.getLevel(pattern, level); pos = this.split(pattern, level, this.DASH , pos); pos = this.split(pattern, level, this.SLASH, pos); return pos; } this.getEnd = function(pattern,level) { return this.getStart(pattern,level); } this.split = function(pattern, level, char, pos) { var newPos = pos; var split = pattern.split(char); if (split.length > 1) { var ps = this.getPos(split, level); if (ps != -1) { newPos = ps; // Found a match, use it } } else { // Ignore } return newPos; } this.getLevel = function(pattern, level) { var pos = -1; if (pattern.indexOf(this.DASH) === -1 && pattern.indexOf(this.SLASH) === -1) { pos = 0; switch (level) { case 1: pos++; case 2: pos += level; break; case 3: pos++; case 4: pos += level; break; case 5: pos = pattern.length; break; } } return pos; } this.getPos = function(split, level) { var pos = -1; if (level >= 0) { pos = 0; } if (level > 0 && split.length > 0) { pos += split[0].length; } if (level > 1 && split.length > 0) { pos += 1; // Dash or Slash } if (level > 2 && split.length > 1) { pos += split[1].length; } if (level > 3 && split.length > 1) { pos += 1; // Dash or Slash } if (level > 4 && split.length > 2) { pos += split[2].length; } if (level > 5 && split.length > 2) { // Ignore } return pos; } this.parse = function(date) { var day = date.getDate(); if (day < 10) { day = '0' + day; } var month = date.getMonth() + 1; // Zero based if (month < 10) { month = '0' + month; } var year = date.getFullYear(); var seperator = this.locale.getSeperator(); // TODO this.SHORT or this.LONG return day + seperator + month + seperator + year; } }