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
関連記事
-
-
formで複数選択可能なセレクトメニュー(プルダウン)を実装する方法(select2)
フォームで複数選択可能なセレクトボックス(プルダウン)を実装したい。ただHTML ...
-
-
jQueryにて特定のIDが存在するかチェックし分岐処理する方法
jQueryにて特定のIDが存在するかチェックし分岐処理したいというケースがあっ ...
-
-
指定した要素に注釈を設定できる「Chardin.js」プラグインの利用方法
指定した要素に注釈を入れることができるjQueryプラグイン「Chardin.j ...
-
-
JavaScriptでURLのクエリを取得する2
直近で調べたのとは別に、 http://aaa.bbb/ccc/id123?te ...
-
-
バニラJavaScriptでdisplay:none/blockの切り替え方法
指定要素の表示非表示を切り替える場合jQueryだとhide/show使うか、t ...