This function is often used to share with everyone.
Function code:
Copy Code code as follows:
Date.prototype.format = function (format) {
var o = {
"m+": This.getmonth () +1,//month
"D+": This.getdate (),//day
"h+": this.gethours (),//hour
"m+": This.getminutes (),//minute
"S+": This.getseconds (),//second
"q+": Math.floor (This.getmonth () +3)/3),//quarter
"S": This.getmilliseconds ()//millisecond
}
if (/(y+)/.test (format)) Format=format.replace (regexp.$1,
(This.getfullyear () + ""). substr (4-regexp.$1.length));
For (var k in O) if (new RegExp ("+ K +")). Test (format)
Format = Format.replace (regexp.$1,
Regexp.$1.length==1? O[K]:
("+ o[k]"). substr (("" + o[k]). length);
return format;
}
Call Example:
Copy Code code as follows:
<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" ></div>
<div id= "Time" ></div>