我想要做的是讓我的閏年功能只返回年份值,當用戶以MM/DD/YYYY的形式輸入日期時。現在,我只用了一年時間,但我希望函數能夠要求用戶輸入該表單中的日期,並計算它是否爲閏年。如何修改我的當前代碼來執行此操作。我已經用split方法聲明瞭一個新變量。謝謝!拆分方法:返回不同部分
function isLeaper() {
var year = document.getElementById("isLeaper").value;
var splitYear = year.split ('/');
// 1. If the year is divisible by 4, but not 100.
if ((parseInt(splitYear) % 4) == 0) {
if (parseInt(splitYear) % 100 == 0) {
if (parseInt(splitYear) % 400 != 0) {
alert(year + 'is not a leap year. Sorry!');
return "false";
}
if (parseInt(splitYear) % 400 == 0) {
alert(year + 'is a leap year. Hooray!');
return "true";
}
}
if (parseInt(splitYear) % 100 != 0) {
alert(year + 'is a leap year. Hooray!');
return "true";
}
}
if ((parseInt(splitYear) % 4) != 0) {
alert(year + 'is not a leap year. Sorry!');
return "false";
}
}
嘗試'console.log(splitYear)',你可能會發現。 – Passerby 2013-03-18 03:49:59