简单教程
提交运行
代码编辑器:
<p id="demo">单击按钮显示UTC时间:</p> <button onclick="myFunction()">点我</button> <script> function addZero(x,n){ if (x.toString().length<n) { x="0" + x; } return x; } function myFunction(){ var d=new Date(); var x=document.getElementById("demo"); var h=addZero(d.getUTCHours(),2); var m=addZero(d.getUTCMinutes(),2); var s=addZero(d.getUTCSeconds(),2); var ms=addZero(d.getUTCMilliseconds(),3); x.innerHTML=h + ":" + m + ":" + s + ":" + ms; } </script>
运行结果: