我正在寻找一种方法来查找出生年份和今天之间的闰年列表并打印出来。
function listOfLeapYears(aYear)
{
aYear = birthDate.getFullYear()
bYear = today.getFullYear()
document.write(aYear + '<br>')
document.write(bYear + '<br>')
for (i = aYear; i <= bYear; )
{
if (isLeapYear(birthDate))
{
document.write(i + '<br>');
}
birthDate.setFullYear(i+1)
}
}
var birthYear, birthMonth, birthDay
birthYear = parseFloat(window.prompt('please enter your birth year' + ''))
birthMonth = parseFloat(window.prompt('please enter your birth month' + ''))
birthDay = parseFloat(window.prompt('please enter your birth day' + ''))
birthDate = new Date(birthYear, birthMonth-1, birthDay)
today = new Date()
document.write('your birth date is ' + dateStringLong(birthDate) + '<br>')
document.write('you are ' + differenceInYears(today, birthDate) + ' years old' + '<br>' )
listOfLeapYears(birthDate)
if语句的for循环似乎没有按预期工作。我想让我的程序做什么,例如:
birthyear是1991年
今天= 2016年是1991年的闰年=&gt;是打印它
=&GT;没有移动到明年是2016年的飞跃是=&gt;是打印它
=&GT;没有停止(因为它等于今天的年份)
答案 0 :(得分:0)
我认为您可以通过这种方式缩小代码:
function listOfLeapYears(birthYear) {
var
leapYear,
count = 0,
i;
leapYear = function(year) {
// leapYear function source: http://stackoverflow.com/questions/16353211/check-if-year-is-leap-year-in-javascript?answertab=active#tab-top
return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
};
i = new Date().getFullYear();
for (; i >= birthYear; i--) {
if (leapYear(i)) {
count++;
document.write(i + '<br>');
}
}
document.write('<br>Count: ' + count);
}
listOfLeapYears(birthYear);
您不需要按月和日来获得闰年列表。
输入:1988年 输出:2016,2012,2008,2004,2000,1996,1992,1988