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
関連記事
-
-
バニラJavaScriptでXML形式データの取り扱いとJSON形式への変換方法
JavaScriptで外部APIにリクエストし、レスポンスのXML形式データを取 ...
-
-
JavaScript / jQueryにてページ表示時に指定したテキストボックスにフォーカスさせる方法
ページを開いた際に指定したテキストボックス(input type="text") ...
-
-
jquery.cookie.jsで「$.cookie is not a function」エラー
フロントエンド側でCookieを利用したい場合jquery-cookieを用いる ...
-
-
クロスドメイン突破をjavascriptで行う
ajaxとかでクロスドメインを突破する際は、 PHPファイルを介すかJSONPを ...
-
-
JavaScriptでテンプレート文字列(テンプレートリテラル)の利用方法
あるJavaScriptのソースコードを拝見した際に変数っぽいのにバッククォート ...