LoginSignup
6
4

More than 5 years have passed since last update.

ActionScriptで生年月日から年齢を計算する簡単ロジック

Last updated at Posted at 2012-12-26

年齢 = floor((今日-誕生日)/10000) で計算できる

var today:Date = new Date();
var y1:String = paddingZero(today.getFullYear(), 4);
var m1:String = paddingZero(today.getMonth() + 1 , 2);
var d1:String = paddingZero(today.getDate(), 2);

var birth:Date = new Date(1983, 2, 16)
var y2:String = paddingZero(birth.getFullYear(), 4);
var m2:String = paddingZero(birth.getMonth() + 1 , 2);
var d2:String = paddingZero(birth.getDate(), 2);

var age:int = Math.ceil((int(y1 + m1 + d1) - int(y2 + m2 + d2)) / 10000);

trace(age);


function paddingZero(num, digit):String{
  return ('0000' + num).slice(-1 * digit);
}

変更履歴

  • 平成29年3月9日 サンプルコードにProgressionフレームワークを利用していましたが、非依存にしました。
6
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
4