เนื่องจากมักจำเป็นต้องจัดรูปแบบวันที่ในการทำงานซึ่งแตกต่างจากแบ็กเอนด์จึงมีวิธีการโทรที่สะดวกและคุณสามารถกำหนดวิธีการจัดรูปแบบในต้นแบบวัตถุวันที่ดูดังนี้:
การคัดลอกรหัสมีดังนี้:
// เพิ่มวิธีการจัดรูปแบบสำหรับต้นแบบวันที่และเวลา
date.prototype.format = function (formatstr) {
var str = formatstr;
var Week = ['Day', 'One', 'Two', 'Three', 'Four', 'Five', 'Six'];
str = str.replace (/yyyy | yyyy/, this.getlyear ());
str = str.replace (/yy | yy/, (this.geteyear () % 100)> 9? (this.geteyear () % 100) .tostring (): '0' +
var month = this.getMonth () + 1;
str = str.replace (/มม./เดือน> 9? เดือน. toString (): '0' + เดือน);
str = str.replace (/m/g, เดือน);
str = str.replace (/w | w/g, สัปดาห์ [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 ());
return str;
-
มันค่อนข้างง่ายในการโทรเช่น:
การคัดลอกรหัสมีดังนี้:
var d = วันที่ใหม่ ();
var str = d.format ("yyyy-mm-dd hh: mm: ss");
console.log (str);