Skip to content

Instantly share code, notes, and snippets.

@wataru218
Created June 4, 2013 16:28
Show Gist options
  • Save wataru218/5707356 to your computer and use it in GitHub Desktop.
Save wataru218/5707356 to your computer and use it in GitHub Desktop.
A CodePen by iw3. 閏年(うるうどし)を調べる - 入力した年が閏年か表示します。 参考: http://ja.wikipedia.org/wiki/閏年 1. 西暦年が4で割り切れる年は閏年 2. ただし、西暦年が100で割り切れる年は平年 3. ただし、西暦年が400で割り切れる年は閏年
<input type="text" id="year" value="2016">年
<input type="button" value="実行" onclick="execute()">
function isLeapYear(year) {
if (year instanceof Date) {
year = year.getFullYear();
}
return ((year % 4 === 0) && (year % 100 !== 0)) || (year % 400 === 0);
}
function execute() {
var value = document.getElementById('year').value;
if (isLeapYear(value)) {
alert(value + ': 閏年');
} else {
alert(value + ': 平年');
}
}
body {
background: #fdd;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment