对于我的一个课程,我们的任务是创建一个利用用户时间的简单javascript函数。我做了一个计算器,告诉你如果你现在就去睡觉什么时候醒来,但是我遇到了输出分钟的问题。
对于分钟,如果它们小于10,我之前加0,所以它的7:05不是7:5。但是因为它将0添加到分钟变量。在我使用分钟
的剩余时间里,它一直停留在那里因此它适用于3:08的0,然后在其余用途中保留它。
下面是我的javascript,我想我只需要缩短变量分钟,只允许保留两个字符,但我不知道该怎么做。
var time = new Date();
var hours = time.getHours();
var minutes = time.getMinutes();
var amPM = "am";
if (hours > 12) {
hours = hours - 12;
amPM = "pm"
}
document.getElementById("currentTime").innerHTML = hours + ':' + minutes + ' ' + amPM;
function checkTime(i) {
if (i < 10) {
i = "0" + i
}; // add zero in front of numbers < 10
return i;
}
function sleepFunction() {
var time = new Date();
var hours = time.getHours();
var minutes = time.getMinutes();
var amPM = "pm";
var fixmin = "0";
if (hours >= 12) {
hours = hours - 12;
amPM = "pm"
}
if (minutes < 10) {
minutes = fixmin + minutes;
}
var hours = hours + 1;
var minutes = minutes + 52;
if (minutes >= 60) {
hours = hours + 1;
minutes = minutes - 60;
}
if (minutes < 10) {
minutes = fixmin + minutes;
}
if (hours >= 12) {
hours = hours - 12;
amPM = "pm"
}
document.getElementById("demo").innerHTML = hours + ':' + minutes + ' ' + amPM;
var hours = hours + 1;
var minutes = minutes + 15;
if (minutes >= 60) {
hours = hours + 1;
minutes = minutes - 60;
}
if (minutes < 10) {
minutes = fixmin + minutes;
}
if (hours >= 12) {
hours = hours - 12;
amPM = "pm"
}
document.getElementById("demo1").innerHTML = hours + ':' + minutes + ' ' + amPM;
var hours = hours + 1;
var minutes = minutes + 30;
if (minutes >= 60) {
hours = hours + 1;
minutes = minutes - 60;
}
if (minutes < 10) {
minutes = fixmin + minutes;
}
if (hours > 12) {
hours = hours - 12;
amPM = "pm"
}
document.getElementById("demo2").innerHTML = hours + ':' + minutes + ' ' + amPM;
var hours = hours + 1;
var minutes = minutes + 30;
if (minutes >= 60) {
hours = hours + 1;
minutes = minutes - 60;
}
if (minutes < 10) {
minutes = fixmin + minutes;
}
if (hours > 12) {
hours = hours - 12;
amPM = "am"
}
document.getElementById("demo3").innerHTML = hours + ':' + minutes + ' ' + amPM;
var hours = hours + 1;
var minutes = minutes + 30;
if (minutes >= 60) {
hours = hours + 1;
minutes = minutes - 60;
}
if (minutes < 10) {
minutes = fixmin + minutes;
}
if (hours > 12) {
hours = hours - 12;
amPM = "am"
}
document.getElementById("demo4").innerHTML = hours + ':' + minutes + ' ' + amPM;
var hours = hours + 1;
var minutes = minutes + 30;
if (minutes >= 60) {
hours = hours + 1;
minutes = minutes - 60;
}
if (minutes < 10) {
minutes = fixmin + minutes;
}
if (hours > 12) {
hours = hours - 12;
amPM = "am"
}
document.getElementById("demo5").innerHTML = hours + ':' + minutes + ' ' + amPM;
document.getElementById("averageWarning").innerHTML = "It Takes the Average Human 18 Minutes to Fall Aleep after viewing a screen";
document.getElementById("sleepNow").innerHTML = "If you go to bed right now, you should set your alarm for one of these times";
}
答案 0 :(得分:0)
鉴于它只是一个显示问题,您可以在任何时候想要显示分钟时使用 checkTime 功能。
例如:
document.getElementById("demo5").innerHTML = hours + ':' + checkTime(minutes) + ' ' + amPM;