Karena sering diperlukan untuk memformat tanggal dalam pekerjaan, tidak seperti backend, ada metode yang nyaman untuk dihubungi, dan Anda dapat mendefinisikan metode format dalam prototipe objek tanggal, lihat sebagai berikut:
Salinan kode adalah sebagai berikut:
// Tambahkan metode pemformatan untuk prototipe tanggal dan waktu
Date.prototype.format = function (formatStr) {
var str = formatstr;
var minggu = ['hari', 'satu', 'dua', 'tiga', 'empat', 'lima', 'enam'];
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 bulan = this.getMonth () + 1;
str = str.replace (/mm/, bulan> 9? Bulan.toString (): '0' + bulan);
str = str.replace (/m/g, bulan);
str = str.replace (/w | w/g, minggu [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 ());
mengembalikan str;
}
Relatif mudah untuk dihubungi, misalnya:
Salinan kode adalah sebagai berikut:
var d = tanggal baru ();
var str = d.format ("yyyy-mm-dd hh: mm: ss");
Console.log (str);