JavaScript 参考手册
JavaScript getUTCDay() 方法
现在,我将创建一个数组,使得上面的例子能够输出星期的名称,而不仅仅是一个数字
源代码:
点击运行 »
<p id="demo">单击按钮显示给定时间中一周的某天(UTC)。</p> <button onclick="myFunction()">点我</button> <script> function myFunction() { var d = new Date(); var weekday = new Array(7); weekday[0] = "周日"; weekday[1] = "周一"; weekday[2] = "周二"; weekday[3] = "周三"; weekday[4] = "周四"; weekday[5] = "周五"; weekday[6] = "周六"; var x = document.getElementById("demo"); x.innerHTML = weekday[d.getUTCDay()]; } </script> <p><strong>注意:</strong> 0=周日, 1=周一, 类推。</p>
运行结果:
点击运行 »