/** * @copyFrom http://Finn-Rasmussen.com/myphp-1.11/myphp-1.11-js/html/javascript/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.11 * @since 27-nov-2009 */ var DateFormat = new function() { this.DASH = '-'; this.COLON = ':'; this.SLASH = '/'; this.SPACE = ' '; this.LONG = 'dd-mm-yyyy hh:mm:ss'; this.SHORT = 'ddmmyy'; this.styleDate = "date"; 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, style) { var pattern = this.adjust(strDate, style); var date = this.newDate(pattern); var format = this.parse(date, style); return format; } this.adjust = function(strDate, style) { 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, this.getHours(), this.getMinutes(), this.getSeconds()); pattern = this.getPattern(now, style, this.SLASH); } else 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, this.getHours(), this.getMinutes(), this.getSeconds()); pattern = this.getPattern(now, style, this.SLASH); } 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, this.getHours(), this.getMinutes(), this.getSeconds()); pattern = this.getPattern(now, style, this.SLASH); } } else 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, this.getHours(), this.getMinutes(), this.getSeconds()); pattern = this.getPattern(now, style, this.SLASH); } } else { //pattern = this.getPattern(now, style, this.SLASH); } return pattern; } this.getPattern = function(date, style, seperator) { var day = date.getDate(); if (day < 10) { day = '0' + day; } var month = date.getMonth() + 1; if (month < 10) { month = '0' + month; } var year = date.getFullYear(); var hour = date.getHours(); if (hour < 10) { hour = '0' + hour; } var min = date.getMinutes(); if (min < 10) { min = '0' + min; } var sec = date.getSeconds(); if (sec < 10) { sec = '0' + sec; } var pattern = '' + day + seperator + month + seperator + year; if (style == this.LONG) { pattern += this.SPACE + hour + this.COLON + min + this.COLON + sec; } return pattern; } this.newDate = function(pattern) { var date = this.getDate(pattern); var month = this.getMonth(pattern); var year = this.getYear(pattern); var hour = this.getHours(pattern); var min = this.getMinutes(pattern); var sec = this.getSeconds(pattern); var newdate = new Date(year, month-1, date, hour, min, sec); return newdate; } this.getSeconds = function(pattern) { var sec = '00'; if (typeof pattern !== 'undefined') { var thePattern = pattern; var newpattern = pattern.split(' '); var colon = pattern.split(this.COLON); if (newpattern.length > 1 && colon.length > 2) { thePattern = newpattern[1]; var start = this.getStart(thePattern, 4, this.COLON); var end = this.getEnd(thePattern, 5, this.COLON); sec = parseInt(thePattern.substring(start, end),10); if (isNaN(sec)) { var today = new Date(); sec = today.getSeconds(); } else { } } } return sec; } this.getMinutes = function(pattern) { var min = '00'; if (typeof pattern !== 'undefined') { var thePattern = pattern; var newpattern = pattern.split(' '); var colon = pattern.split(this.COLON); if (newpattern.length > 1 && colon.length > 2) { thePattern = newpattern[1]; var start = this.getStart(thePattern, 2, this.COLON); var end = this.getEnd(thePattern, 3, this.COLON); min = parseInt(thePattern.substring(start, end),10); if (isNaN(min)) { var today = new Date(); min = today.getMinutes(); } } } return min; } this.getHours = function(pattern) { var hour = '00'; if (typeof pattern !== 'undefined') { var thePattern = pattern; var newpattern = pattern.split(' '); var colon = pattern.split(this.COLON); if (newpattern.length > 1 && colon.length > 2) { thePattern = newpattern[1]; var start = this.getStart(thePattern, 0, this.COLON); var end = this.getEnd(thePattern, 1, this.COLON); hour = parseInt(thePattern.substring(start, end),10); if (isNaN(hour)) { var today = new Date(); hour = today.getHours(); } } } return hour; } 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, char) { var pos = this.getLevel(pattern, level); if (char === this.COLON) { pos = this.split(pattern, level, char , pos); // hh:mm:ss } else { pos = this.split(pattern, level, this.DASH , pos); pos = this.split(pattern, level, this.SLASH, pos); } return pos; } this.getEnd = function(pattern, level, char) { return this.getStart(pattern, level, char); } 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 0: pos += level; break; case 1: pos++; case 2: pos += level; break; case 3: pos++; case 4: pos += level; break; case 5: pos = pattern.length; //pos += level; 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, style) { var seperator = this.locale.getSeperator(); return this.getPattern(date, style, seperator); } }