Oft können wir die integrierten Methoden der Datumsobjekte in JavaScript verwenden, um sie zu formatieren, wie z.
Die Codekopie lautet wie folgt: var d = new Date ();
console.log (d);
console.log (d.todatestring ());
console.log (d.togmtstring ());
console.log (d.toisstring ());
console.log (d.tojson ());
console.log (d.tolocaledateString ());
console.log (d.tolocalestring ());
console.log (d.tolocaletimestring ());
console.log (d.tostring ());
console.log (d.totimestring ());
console.log (d.toutcstring ());
Wenn die obige Methode unsere Anforderungen nicht erfüllen kann, können Sie auch die Funktion anpassen, um die Zeit zu formatieren, z. B.:
Die Codekopie lautet wie folgt:
Datum.Prototype.Format = Funktion (Format) {
var date = {
"M +": this.getMonth () + 1,
"D+": this.getDate (),,
"H+": this.gethours (),
"M+": this.getminutes (),,
"S+": this.getSeconds (),,
"q +": math.floor ((this.getMonth () + 3) / 3),
"S+": this.getMilliseconds ()
};
if (/(y+)/i.test(format)) {
format = format.replace (regexp. $ 1, (this.getingfullyear () + '') .substr (4 - regexp. $ 1.Length));
}
für (var k in Datum) {
if (neuer regexp ("(" + k + ")"). test (format)) {
format = format.replace (regexp. $ 1, regexp. $ 1.Length == 1
? Datum [k]: ("00" + Datum [k]).
}
}
Rückgabeformat;
}
var d = new Date (). Format ('yyyy-mm-dd');
console.log (d);