この機能はよく使用され、共有されます。
関数コード:
コードコピーは次のとおりです。
date.prototype.format = function(format){
var o = {
「M+」:this.getMonth()+1、//月
「D+」:this.getDate()、// day
「H+」:this.gethours()、// hour
「M+」:this.getMinutes()、// minuts
「S+」:this.getSeconds()、// second
「Q+」:math.floor((this.getmonth()+3)/3)、//四半期
「S」:this.getMilliseConds()// millisecond
}
if(/(y+)/。test(format))format = format.replace(regexp。$ 1、
(this.getTullyear()+"")。substr(4 -regexp。$ 1.Length));
for(o in o)if(new 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()。形式( "HH:MM:SS");
var date = new date()。形式( "yyyy-mm-dd");
document.getElementById( "time")。innerhtml = time;
document.getElementById( "date")。innerhtml = date;
}、1000);
</script>
<div id = "date"> </div>
<div id = "time"> </div>