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
関連記事
-
-
円形のプログレスバーをprogressbar.jsで実装する方法
円形のプログレスバーを実装したかった。CSSだったり画像を用いたりする方法もあっ ...
-
-
PHPの配列をファイルに保存してPHPやJavaScriptで呼び出す方法
PHPの配列をファイルとして保存し、別のページからPHPやJavaScriptで ...
-
-
jQuery Nice Selectを特定ページのみ無効にする方法
bootstrap系のテンプレートデザインを使用したサイトを構築中に、セレクトメ ...
-
-
Dexie.jsを利用してIndexedDBを取り扱う方法
IndexedDBを用いて色々するという案件を見かけたが、そもそもIndexed ...
-
-
jQueryで指定した要素を簡単にフォーカスするfocusbleの使い方
divやspanなど指定した要素を簡単にフォーカスするjQueryプラグインがあ ...