【JavaScript】生年月日を入力したら自動的に現在の年齢を入れてくれる

サーバ・プログラム

プロフィールなどで自動で現時点の年齢を入れてくれるプログラム探してて良いサイトがあったので覚書です。

JavaScript だとハマる ~生年月日から年齢を計算する簡単な計算式~

JavaScriptなので、汎用性も高いのでいいよね!!

こちらをコピペして、表示させたいところに入れてね!!

[c]
<script type="text/javascript">
function calculateAge(birthday) {
// birth[0]→year, birth[1]→month, birth[2]→day
var birth = birthday.split(‘-‘);
var _birth = parseInt("" + birth[0] + birth[1] + birth[2]);
var today = new Date();
var _today = parseInt("" + today.getFullYear() + affixZero(today.getMonth() + 1) + affixZero(today.getDate()));
return parseInt((_today – _birth) / 10000);
}

function affixZero(int) {
if (int < 10) int = "0" + int;
return "" + int;
}

// ここに自分の誕生日入れてね
document.write(calculateAge(‘1985-05-04’));

</script>
[/c]

高橋拓郎は現在[sc name=”age”]歳です。

JavaScript 第6版
JavaScript 第6版

posted with amazlet at 13.03.19
David Flanagan
オライリージャパン
売り上げランキング: 8,959

コメント

  1. […] 参考:【JavaScript】生年月日を入力したら自動的に現在の年齢を入れてくれる 参考:JavaScript だとハマる ~生年月日から年齢を計算する簡単な計算式~ […]

タイトルとURLをコピーしました