如何获得“0”分?在“1”;“9”;在一个钟里
How to get a "0" before "1" to "9" in a clock
我的时钟脚本如下。由于某种原因,if(minutes<10){minutes="0"+minutes;}
和if(seconds<10){seconds="0"+seconds;}
会在小于10
的数字前添加0
,但在数小时内不会这样做。有什么建议吗?
<script>
function TimeUpdate() {
var now = new Date(), hours = now.getHours(), minutes = now.getMinutes(), seconds = now.getSeconds();
// The 1st "if" does not work.
if (hours < 10) {hours = "0" + hours;}
if (minutes < 10) {minutes = "0" + minutes;}
if (seconds < 10) {seconds = "0" + seconds;}
// (This works) AM or PM option
if (hours >= 12 && hours < 24) {var TimeOfDay = "PM";}
else {var TimeOfDay = "AM";}
// (This works) Converts the hours from 24 to 12
if (hours > 12) {hours = hours - 12;}
// This sets the hours to a specific number.
// This is used only for this demonstration.
hours = 5;
// (This works) Puts everything together
var CurrentTime = hours + ':' + minutes + ':' + seconds + " " + TimeOfDay;
// (This works) The clock <div>
var MyClock = document.getElementById('clock');
// (This works) Writes the "CurrentTime" to the clock's <div>
MyClock.innerHTML = CurrentTime;
var t = setInterval (function () {TimeUpdate ()}, 1000);
}
// (This works) This loads the clock onto the page.
window.onload = TimeUpdate;
</script>
<p id="clock"></p>
你的支票写满了。把这段代码再往下移动:
function TimeUpdate() {
var now = new Date(),
hours = now.getHours(),
minutes = now.getMinutes(),
seconds = now.getSeconds();
if (minutes < 10) {
minutes = "0" + minutes;
}
if (seconds < 10) {
seconds = "0" + seconds;
}
// (This works) AM or PM option
if (hours >= 12 && hours < 24) {
var TimeOfDay = "PM";
} else {
var TimeOfDay = "AM";
}
// (This works) Converts the hours from 24 to 12
if (hours > 12) {
hours = hours - 12;
}
// This sets the hours to a specific number.
// This is used only for this demonstration.
hours = 5;
// The 1st "if" does not work.
if (hours < 10) {
hours = "0" + hours;
}
// (This works) Puts everything together
var CurrentTime = hours + ':' + minutes + ':' + seconds + " " + TimeOfDay;
// (This works) The clock <div>
var MyClock = document.getElementById('clock');
// (This works) Writes the "CurrentTime" to the clock's <div>
MyClock.innerHTML = CurrentTime;
var t = setInterval(function() {
TimeUpdate()
}, 1000);
}
// (This works) This loads the clock onto the page.
window.onload = TimeUpdate;
<br>
<br>
<br>
<p id="clock"></p>
你需要放置
if (hours < 10) {
hours = "0" + hours;
}
后if (hours > 12) {
hours = hours - 12;
}
因为它必须在13-21小时(1-9 PM)也是正确的。
注意这段代码。这是个递归函数,你很快就会耗尽你的电脑内存。请将TimeUpdate()
从自身内部移除
function TimeUpdate() {
var now = new Date(),
hours = now.getHours(),
minutes = now.getMinutes(),
seconds = now.getSeconds();
if (minutes < 10) {
minutes = "0" + minutes;
}
if (seconds < 10) {
seconds = "0" + seconds;
}
// (This works) AM or PM option
if (hours >= 12 && hours < 24) {
var TimeOfDay = "PM";
} else {
var TimeOfDay = "AM";
}
// (This works) Converts the hours from 24 to 12
if (hours > 12) {
hours = hours - 12;
}
// This sets the hours to a specific number.
// This is used only for this demonstration.
hours = 5;
// The 1st "if" does not work.
if (hours < 10) {
hours = "0" + hours;
}
// (This works) Puts everything together
var CurrentTime = hours + ':' + minutes + ':' + seconds + " " + TimeOfDay;
// (This works) The clock <div>
var MyClock = document.getElementById('clock');
// (This works) Writes the "CurrentTime" to the clock's <div>
MyClock.innerHTML = CurrentTime;
}
// (This works) This loads the clock onto the page.
var t = setInterval(function() {
TimeUpdate()
}, 1000);
<br>
<br>
<br>
<p id="clock"></p>
相关文章:
- 有一个带有文字的红色虚线框.如何定位当前日期(在 JavaScript 中?在那个盒子里
- 如何在一页上多次使用一个图1次莫里斯JS
- 选择一组三里并将它们显示在下一个或上一个按钮上
- 在开关箱里放一个开关箱是不是不好的做法
- .wrap除了普通包装纸里的一个以外的所有东西
- 必须创建一个简单的计算器,使用eval()进行计算.我可以'我不知道如何把答案发到文本框里
- 左和右在一个里有一个边界
- 在一个灯箱里,屏幕上并排显示着两幅图像
- 如何动态检索输入类型"url"把它放到一个变量里
- 在一个圆圈里画形状
- 如何拖拽一个里到另一个里与各自的图像
- 如何传递一个名称来形成动作在胸里叶
- 把一个指令绑定到另一个指令里的方式是什么?
- 一个箱子里只放一个搬运工
- 在一个元素中合并两个莫里斯图
- 我可以把一个对象和一个数组放在另一个数组里吗
- Javascript:多个GetElementByID's在一个电话里
- SyntaxError: missing;前语句:试着做一个钟
- 如何获得“0”分?在“1”;“9”;在一个钟里
- 的承诺.都在另一个承诺里.一切似乎在完成之前就退出了,并显示出不从承诺中返回的警告