var utilDateTimeFormat = "d.M.yyyy"; function utilParseDate(str) { var yindex = utilDateTimeFormat.indexOf("y"); var dindex = utilDateTimeFormat.indexOf("d"); var mindex = utilDateTimeFormat.indexOf("M"); if (yindex < mindex && yindex < dindex) { yindex = 0; if (dindex < mindex) { dindex = 1; mindex = 2; } else { mindex = 1; dindex = 2; } } else if (mindex < yindex && mindex < dindex) { mindex = 0; if (dindex < yindex) { dindex = 1; yindex = 2; } else { yindex = 1; dindex = 2; } } else { dindex = 0; if (mindex < yindex) { mindex = 1; yindex = 2; } else { yindex = 1; mindex = 2; } } var arr = str.split(new RegExp("[^0-9]+")); if (arr[0] == "") arr.shift(); if (arr.length < 3) return null; return [new Number(arr[yindex]), new Number(arr[mindex]), new Number(arr[dindex])]; } function utilAddYearSubDay(date) { date = [date[0]+1,date[1],date[2]-1]; if (date[2] == 0) { date[1] --; if (date[1] == 0) { date[0] --; date[1] = 12; } var days = [31,28+(utilIsLeapYear(date[0]) ? 1 : 0),31,30,31,30,31,31,30,31,30,31]; date[2] = days[date[1]-1]; } return date; } function utilGetDatePart(date,part) { if (part == "d") { return ""+date[2]; } else if (part == "dd") { return ""+((date[2] < 10) ? "0"+date[2] : date[2]); } else if (part == "M") { return ""+date[1]; } else if (part == "MM") { return ""+((date[1] < 10) ? "0"+date[1] : date[1]); } else if (part == "y" || part == "yy") { return ""+((date[0] % 100) < 10 ? "0" + (date[0] % 100) : date[0] % 100); } else if (part == "yyyy") { return ""+date[0]; } return "["+part+"]"; } function utilFormatDate(date) { var result = ""; var rest = utilDateTimeFormat; var idx; while ((idx = rest.search(new RegExp("[^a-zA-Z]"))) >= 0) { if (idx == 0) { // literal result += rest.substr(0,1); rest = rest.substr(1,rest.length-1); } else { result += utilGetDatePart(date,rest.substr(0,idx)); rest = rest.substr(idx,rest.length-idx); } } return result + utilGetDatePart(date,rest); } function utilIsLeapYear(year) { if (year % 4 != 0) return false; if (year % 100 != 0) return true; return year % 400 == 0; } function utilIsValidDate(date) { var year = date[0]; var month = date[1]; var day = date[2]; if (month < 1 || month > 12 || day < 1 || day > 31) return false; if (day == 31 && (month == 2 || month == 4 || month == 6 || month == 9 || month == 11)) return false; if (day == 30 && month == 2) return false; if (day == 29 && month == 2 && !utilIsLeapYear(year)) return false; return true; } function utilGetDateFromRC(rodneCislo) { var rc = rodneCislo.replace(new RegExp("[\\s/]",'g'),"") if (!rc.match(new RegExp("\\d\\d\\d\\d\\d\\d"))) return null; var year = parseInt(rc.substr(0,2),10); var month = parseInt(rc.substr(2,2),10); var day = parseInt(rc.substr(4,2),10); if (month > 50) month -= 50; month %= 20; if (rc.length == 9 || year >= 54) year += 1900; else year += 2000; var date = [year,month,day]; if (utilIsValidDate(date)) return date; else return null; }