ฟังก์ชั่นนี้มักจะใช้และแบ่งปันกับคุณ
รหัสฟังก์ชัน:
การคัดลอกรหัสมีดังนี้:
date.prototype.format = function (รูปแบบ) {
var o = {
"M+": this.getMonth ()+1, // เดือน
"d+": this.getdate (), // วัน
"H+": this.getHours (), // ชั่วโมง
"M+": this.getMinutes (), // นาที
"S+": this.getSeconds (), // วินาที
"Q+": Math.floor ((this.getMonth ()+3)/3), // ไตรมาส
"s": this.getmilliseconds () // มิลลิวินาที
-
if (/(y+)/. ทดสอบ (รูปแบบ)) รูปแบบ = format.replace (regexp. $ 1,
(this.getlyear ()+""). substr (4 - regexp. $ 1. ความยาว));
สำหรับ (var k ใน o) ถ้า (ใหม่ regexp ("(" + k + ")"). การทดสอบ (รูปแบบ))
format = format.replace (regexp. $ 1
regexp. $ 1. ความยาว == 1? ตกลง] :
("00"+ o [k]). substr ((""+ o [k]). ความยาว));
รูปแบบการส่งคืน;
-
ตัวอย่างการโทร:
คัดลอกรหัสดังนี้: <script type = "text/javascript">
setInterval (function () {
var time = new Date (). รูปแบบ ("HH: MM: SS");
วันที่ var = วันที่ใหม่ (). รูปแบบ ("yyyy-mm-dd");
document.getElementById ("เวลา"). innerhtml = เวลา;
document.getElementById ("วันที่"). innerhtml = วันที่;
}, 1,000);
</script>
<div id = "วันที่"> </div>
<div id = "เวลา"> </div>