نظرًا لأنه من الضروري في كثير من الأحيان تنسيق التواريخ في العمل ، على عكس الواجهة الخلفية ، هناك طرق ملائمة للاتصال بها ، ويمكنك تحديد طريقة التنسيق في النموذج الأولي لكائن التاريخ ، انظر على النحو التالي:
نسخة الكود كما يلي:
// إضافة طريقة التنسيق للتاريخ والوقت النموذج الأولي
date.prototype.format = function (formatstr) {
var str = formatstr ؛
var week = ['Day' ، 'One' ، 'اثنين' ، 'Three' ، 'Four' ، 'Five' ، '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 month = this.getMonth () + 1 ؛
str = str.replace (/mm/، month> 9؟ month.toString (): '0' + month) ؛
str = str.replace (/m/g ، month) ؛
str = str.replace (/w | w/g ، week [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 ()) ؛
إرجاع شارع
}
من السهل نسبيًا الاتصال ، على سبيل المثال:
نسخة الكود كما يلي:
var d = new Date () ؛
var str = d.format ("yyyy-mm-dd hh: mm: ss") ؛
console.log (str) ؛