Puisqu'il est souvent nécessaire de formater les dates de travail, contrairement au backend, il existe des méthodes pratiques à appeler, et vous pouvez définir une méthode de format dans le prototype d'objet date, voir comme suit:
La copie de code est la suivante:
// Ajouter une méthode de formatage pour le prototype de date et d'heure
Date.prototype.format = fonction (formatstr) {
var str = formattr;
var semaine = [«jour», «un», «deux», «trois», «quatre», «cinq», «six»];
str = str.replace (/ yyyy | yyyy /, this.getlyear ());
str = str.replace (/ yy | yy /, (this.getyear ()% 100)> 9? (this.getyear ()% 100) .toString (): '0' + (this.getyear ()% 100));
var mois = this.getMonth () + 1;
str = str.replace (/ mm /, mois> 9? mois.toString (): '0' + mois);
str = str.replace (/ m / g, mois);
str = str.replace (/ w | w / g, semaine [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 ());
retour STR;
}
Il est relativement simple d'appeler, par exemple:
La copie de code est la suivante:
var d = new Date ();
var str = d.format ("yyyy-mm-dd hh: mm: ss");
Console.log (STR);