Da es häufig erforderlich ist, Daten in der Arbeit zu formatieren, sind im Gegensatz zum Backend bequeme Methoden zu rufen, und Sie können eine Formatmethode im Datum Objektprototyp definieren, siehe wie folgt:
Die Codekopie lautet wie folgt:
// Formatierungsmethode für Datum und Uhrzeitprototyp hinzufügen
Datum.Prototype.Format = Funktion (formatstr) {
var str = formatstr;
var week = ['Tag', 'eins', 'zwei', 'drei', 'vier', 'fünf', 'sechs'];
str = str.replace (/yyyy | yyyy/, this.getingfoyear ());
Str = Str.Replace (/yy | yy/, (this.getyear () % 100)> 9? (this.getyear () % 100) .toString (): '0' + (this.getyear () % 100));
var monat = this.getMonth () + 1;
str = str.replace (/mm/, Monat> 9? Monat.ToString (): '0' + Monat);
Str = Str.Replace (/m/g, Monat);
str = str.replace (/w | w/g, Woche [this.getay ()]);
str = str.replace (/dd | dd/, this.getDate ()> 9? this.getDate (). toString (): '0' + this.getDate ());
str = str.replace (/d | d/g, this.getDate ());
str = str.replace (/hh | hh/, this.gethours ()> 9? this.gethours (). toString (): '0' + this.gethours ());
str = str.replace (/h | h/g, this.gethours ());
str = str.replace (/mm/, this.getminutes ()> 9? this.getminutes (). toString (): '0' + this.getminutes ());
str = str.replace (/m/g, this.getminutes ());
str = str.replace (/ss | ss/, this.getSeconds ()> 9? this.getSeconds (). toString (): '0' + this.getSeconds ());
str = str.replace (/s | s/g, this.getSeconds ());
return str;
}
Es ist relativ einfach anzurufen, zum Beispiel:
Die Codekopie lautet wie folgt:
var d = neues Datum ();
var str = d.Format ("yyyy-mm-dd hh: mm: ss");
console.log (str);