غالبًا ما يتم استخدام هذه الوظيفة ومشاركتها معك.
رمز الوظيفة:
نسخة الكود كما يلي:
date.prototype.format = function (format) {
var o = {
"M+": this.getmonth ()+1 ، // month
"D+": this.getDate () ، // day
"H+": this.gethours () ، // ساعة
"M+": this.getMinutes () ، // دقيقة
"S+": this.getSeconds () ، // الثانية
"Q+": Math.Floor ((this.getMonth ()+3)/3) ، // Quarter
"S": this.getmilliseconds () // millisecond
}
إذا (/(y+)/. اختبار (تنسيق)) تنسيق = format.replace (regexp. $ 1 ،
(this.getlyear ()+""). substr (4 - regexp. $ 1.Length)) ؛
لـ (var k in o) if (regexp جديد ("(" + k + ")"). اختبار (تنسيق)))
format = format.replace (regexp. $ 1 ،
regexp. $ 1.length == 1؟ نعم] :
("00"+ o [k]). substr ((""+ o [k]). الطول)) ؛
تنسيق العودة
}
مثال اتصل على:
انسخ الرمز كما يلي: <script type = "text/javaScript">
setInterval (function () {
var time = new Date (). format ("HH: mm: ss") ؛
var date = new Date (). format ("Yyyy-MM-DD") ؛
document.getElementById ("time"). innerhtml = time ؛
document.getElementById ("Date"). innerhtml = date ؛
} ، 1000) ؛
</script>
<div id = "date"> </viv>
<div id = "time"> </viv>