JavaScriptで年齢計算
2014/05/22
JavaScriptで年齢計算。
■ソース
function ageCalculator(selectY, selectM, selectD) {
var dateObj = new Date();
var today = parseInt("" + dateObj.getFullYear() + zeroformat(dateObj.getMonth() + 1) + zeroformat(dateObj.getDate()));
var birthday = parseInt("" + selectY + zeroformat(selectM) + zeroformat(selectD));
var age = (today - birthday) / 10000;
return parseInt(age);
}
function zeroformat(v) {
return (String(v).length == 1) ? '0' + v : v;
}
console.log(ageCalculator (2000, 01, 01)); //13
関連記事
-
-
jQueryのanimate()でページスクロール後に他の処理を実行させる方法
jQueryのanimate()でページスクロール後に特定の要素を非表示にしたい ...
-
-
Leafletでマーカーを動的に変更し、変更状況をCookieに保存する方法
Leafletでポップアップ(フキダシ)内のボタンをクリックするとマーカーを動的 ...
-
-
jQueryにて複数のajax処理のレスポンスをまとめて表示する方法
同時にajax処理を行う際、挙動やコードの書き方に悩まされることが多い。例えばA ...
-
-
CodeIgniter4&Bootstrap&jQueryで簡易版お問い合わせページの作成
CodeIgniter4.4.4&Bootstrap&jQuer ...
-
-
jQueryでHTMLの特定要素をループ時に逆順で処理する方法
jQueryでリスト(li)タグ等の特定要素をeach()でループ処理する場合、 ...