Muchas veces, podemos usar los métodos incorporados de objetos de fecha en JavaScript para formatearlos, como:
La copia del código es la siguiente: var d = new Date ();
console.log (D);
console.log (d.todateString ());
console.log (D.TogmtString ());
console.log (d.toisoString ());
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 ());
Si el método anterior no puede cumplir con nuestros requisitos, también puede personalizar la función para formatear el tiempo, como:
La copia del código es la siguiente:
Date.prototype.format = function (format) {
var fecha = {
"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 = formato
}
para (var k en fecha) {
if (new Regexp ("(" + K + ")"). Test (Format)) {
format = format.replace (regexp. $ 1, regexp. $ 1.length == 1
?
}
}
formato de retorno;
}
var d = new Date (). Format ('yyyy-mm-dd');
console.log (d);