Dado que a menudo es necesario formatear las fechas en el trabajo, a diferencia del backend, hay métodos convenientes para llamar, y puede definir un método de formato en el prototipo de objeto de fecha, ver lo siguiente:
La copia del código es la siguiente:
// Agregar método de formato para el prototipo de fecha y hora
Date.prototype.format = function (formatstr) {
var str = formatstr;
var Week = ['Day', 'One', 'Two', 'Three', 'Four', 'Five', 'Six'];
str = str.replace (/yyyy | yyyy/, this.getblyar ());
str = str.replace (/yy | yy/, (this.getyear () % 100)> 9? (this.getyear () % 100) .ToString (): '0' + (this.getyear () % 100));
var mes = this.getMonth () + 1;
str = str.replace (/mm/, mes> 9? mes.ToString (): '0' + mes);
str = str.replace (/m/g, mes);
str = str.replace (/w | w/g, semana [this.getday ()]);
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 ());
regresar str;
}
Es relativamente simple llamar, por ejemplo:
La copia del código es la siguiente:
var d = nueva fecha ();
var str = d.format ("aaa-mm-dd hh: mm: ss");
console.log (str);