Hallo liebe Leute,
fange gerade an, mich für das Aufbauen einer eigenen kleinen library zu begeistern.
U.a. möchte ich dem Date-Objekt eine neue Methode zuweisen, da ich aber in Sachen
Prototyping noch unerfahren bin, interessiert mich Eure Meinung, ob der Code gut
geschrieben ist, oder ob man den (viel) besser/sinnvoller schreiben kann:
Array.prototype.kill = function($) {
var m;
for (m=0;m<this.length;m++) {
if (m >= $) {
this[m] = this[m + 1];
}
}
this.length = this.length-1;
};
Date.prototype.toLocalDate = function(date,lang) {
var i,
results = [],
year = this.getYear(),
Year = this.getFullYear(),
month = this.getMonth() + 1,
day = this.getDate(),
weekDay = this.getDay(),
hour = this.getHours(),
minute = this.getMinutes(),
second = this.getSeconds(),
wochentage = ["Sonntag","Montag","Dienstag","Mittwoch","Donnerstag","Freitag","Samstag"],
weekdays = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"],
diasdelasemana = ["Domingo","Lunes","Martes","Miercoles","Jueves","Viernes","Sabado"],
monate = ["Januar","Februar","März","April","Mai","Juni","Juli","August","September","Oktober","November","Dezember"],
months = ["January","February","March","April","May","June","July","August","Septembre","Octobre","Novembre","Decembre"],
meses = ["Enero","Febrero","Marzo","Abril","Mayo","Junio","Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre"],
val = date,
search1 = /\W/g,
search2 = /\w*/gi,
punc = val.match(search1),
settings = val.match(search2);
settings.length = settings.length - 1;
for (i = 0; i<settings.length; i++) {
if (settings[i] != "" && settings[i] != ",") {
results.push(evaluateSetting(settings[i]));
} else { results.push(punc[0]); punc.kill(0); }
}
function evaluateSetting(val) {
switch (val) {
case "yyyy" : return Year; break;
case "yy" : return year; break;
case "mm" : return fix(month); break;
case "m" : return month; break;
case "month" : if (lang == "deu") { return monate[month]; }
else if (lang == "eng") { return months[month]; }
else if (lang == "esp") { return meses[month]; } break;
case "dd" : return fix(day); break;
case "d" : return day; break;
case "wd" : if (lang == "deu") { return wochentage[weekDay]; }
else if (lang == "eng") { return weekdays[weekDay]; }
else if (lang == "esp") { return diasdelasemana[weekDay]; } break;
case "hh" : return fix(hour); break;
case "h" : return hour; break;
case "minmin" : return fix(minute); break;
case "min" : return minute; break;
case "ss" : return fix(second); break;
case "s" : return second; break;
default : return false;
}
};
function fix(val) {
if (val.toString().length == 1) {
val = "0" + val;
}
return val;
};
var localDate = results.join('');
return localDate;
};
var datum = new Date();
alert(datum.toLocalDate('wd, d. month yyyy, hh:minmin','esp'));
Vielen Dank!!