Skip to content

Instantly share code, notes, and snippets.

@satoshin2071
Created March 5, 2013 10:00
Show Gist options
  • Save satoshin2071/5089209 to your computer and use it in GitHub Desktop.
Save satoshin2071/5089209 to your computer and use it in GitHub Desktop.
rubyで生年月日から年齢を抽出する。
#!/usr/bin/ruby
#現在の日時
now_date = Time.now
#誕生日
birth_date = Time.parse(1972,8,10)
#現在の日時と誕生日の差分を出し、年数に換算し、さらにフォーマット関数で年だけを表示
my_age = sprintf("%d",((now_date - birth_date) / 60 / 60 / 24)/365)
p my_age
## 実行結果
34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment