我正在尝试显示下一个生日的天,小时,分钟和秒。生日是通过提示值计算的。我计算了秒,分,小时和天。但是我得到了错误的价值,例如" 0&#34 ;.我的代码有什么问题?
代码:
<!DOCTYPE html>
<html>
<head>
<style>
p{
text-align: center;
font-family: monospace;
font-size: 20px;
}
</style>
<title>trail</title>
</head>
<body>
<h1 style="text-align: center; font-family: monospace;"> My age </h1>
<p id="dateField"> </p>
<p id="birthField"> </p>
<p id="nextBirth"> </p>
<script>
var monthNames = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
];
var days = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
var today = new Date();
var myMonth = prompt("What month were you born in?");
var myDay = prompt("What day were you born on?");
var myYear = prompt("What year were you born in?");
var birthday = new Date(myYear, myMonth, myDay);
var dateLoc = document.getElementById("dateField").innerHTML = "Today is " + today;
var birthField = document.getElementById("birthField")
.innerHTML = "I was born on " + monthNames[myMonth - 1] + ' ' + myDay + ',' + myYear + '.'
+ '(' + myMonth + '/' + myDay + '/' + myYear + ')';
var years = today.getFullYear() - birthday.getFullYear();
var seconds = Math.floor( (years/1000) % 60 );
var minutes = Math.floor( (years/1000/60) % 60 );
var hours = Math.floor( (years/(1000*60*60)) % 24 );
var days = Math.floor( years/(1000*60*60*24) );
var nextBirth = document.getElementById("nextBirth").innerHTML = "I am " + years + " Years old, and will turn " + (years + 1 ) + " in:" + "<br>" + days + " days," + hours + "hours," + minutes + "minutes," + seconds + "seconds";
</script>
</body>
</html>
答案 0 :(得分:0)
(不考虑闰年/秒等)
使用new Date()
过去的月份从1月开始为0。你可以使用:
var myMonth = prompt("What month were you born in?") - 1;
现在您的变量birthday
将存储正确的月份。
对于birthField
,您可以将monthNames[my Month - 1]
更改为monthNames[my Month]
对于years
,您只是减去当年的出生年份。如果今年的生日没有发生,years
将会关闭。我们稍后会解决这个问题。
我们需要找出用户下一个生日的正确日期。这是今年或明年。将今年生日的日期存储在变量中可能很方便:
var thisYearsBirthday = new Date(today.getFullYear(), myMonth, myDay);
我们可以存储用户即将到来的生日年份的变量,假设它是明年:
var nextBirthdayYear = today.getFullYear() + 1;
然后,我们可以检查将来是否thisYearsBirthday
,如果是,那么今年将nextBirthdayYear
更改为。我们还应该更改years
以反映用户今年的生日还没有发生。
if ( today < thisYearsBirthday ) {
nextBirthdayYear = today.getFullYear();
years = years - 1;
}
现在我们可以存储用户下一个正确的生日:
var nextBirthday = new Date( nextBirthdayYear, myMonth, myDay );
我们可以存储在用户下一个生日之前剩余的毫秒数:
var milliseconds = nextBirthday - today;
您的变量seconds
,minutes
,hours
和days
不是基于年的公式,而是基于毫秒。我们应该将milliseconds
变量放在那里:
var seconds = Math.floor( (milliseconds/1000) % 60 );
var minutes = Math.floor( (milliseconds/1000/60) % 60 );
var hours = Math.floor( (milliseconds/(1000*60*60)) % 24 );
var days = Math.floor( milliseconds/(1000*60*60*24) );
这将导致显示下一个生日的正确天数,小时数,分钟数和秒数。
答案 1 :(得分:-1)
嗨,你错过了计算下一个生日,它应该低于代码
var today = new Date();
var myMonth = prompt("What month were you born in?");
var myDay = prompt("What day were you born on?");
var myYear = prompt("What year were you born in?");
var birthday = new Date(myYear, myMonth, myDay);
console.log(birthday);
var dateLoc = document.getElementById("dateField").innerHTML = "Today is " + today;
var birthField = document.getElementById("birthField")
.innerHTML = "I was born on " + monthNames[myMonth - 1] + ' ' + myDay + ',' + myYear + '.'
+ '(' + myMonth + '/' + myDay + '/' + myYear + ')';
var milliseconds,seconds,minutes,hours,days;
var years = today.getFullYear() - birthday.getFullYear();
//Here is my code
myMonth--;// Because of Month index is 0-11
var nextBirthday = (new Date(today.getFullYear(), myMonth, myDay))<today?(new Date(today.getFullYear()+1, myMonth, myDay)):(new Date(today.getFullYear(), myMonth, myDay)) //checking is passed or not
var diff = nextBirthday-today;
milliseconds=diff%1000;
diff=(diff-(milliseconds))/1000;
console.log(diff);
seconds=diff%60
console.log(diff);
diff=(diff-seconds)/60;
console.log(diff);
minutes=diff%60
diff=(diff-minutes)/60;
console.log(diff);
hours=diff%24
days=(diff-hours)/24;
console.log(diff);
var nextBirth = document.getElementById("nextBirth").innerHTML = "I am " + years + " Years old, and will turn " + (years + 1 ) + " in:" + "<br>" + days + " days," + hours + "hours," + minutes + "minutes," + seconds + "seconds";
您可以在
下找到工作代码popFirst()
答案 2 :(得分:-2)
所以你要在下一个生日那天得到几天,几小时,几分钟和几秒钟。问题是你的等式基于太少的信息。这是问题所在。
var years = today.getFullYear() - birthday.getFullYear();
//with example values: 30 = 2017 - 1987
//Here you try to get values from 30
var seconds = Math.floor( (years/1000) % 60 );
var minutes = Math.floor( (years/1000/60) % 60 );
var hours = Math.floor( (years/(1000*60*60) ) % 24 );
var days = Math.floor( years/(1000*60*60*24) );
麻烦的是Date.prototype.getFullYear()只返回年份,所以从另一个中减去一个'fullYear'只会给你年龄,但不会返回任何其他信息。要获得您想要的结果,您需要找到一些方法来传递更多信息。我的建议是添加如下内容。
var years = today.getFullYear() - birthday.getFullYear();
//Edit: it's been pointed out that not everyone's next birthday is next year. This solution still works, but we need to add conditional logic to this next statement.
if ( today.getMonth() > birthday.getMonth() || (today.getMonth() === birthday.getMonth() && today.getDay() >= birthday.getDay() )) {
var nextYear = today.getFullYear()+1;
}
var nextBirthday = birthday;
nextBirthday.setFullYear(nextYear);
//This converts nextBithday into milliseconds since 1970. Use Date.getTime() to do the same to today and subtract.
var timeToNextBday = nextBirthday - today.getTime();
//From here use math to get the precise days and time until your next birthday