LoginSignup
0

More than 3 years have passed since last update.

年齢を計算する

Last updated at Posted at 2019-06-14

Date型から年齢の整数に変換するコードを毎回書いている気がします

ruby

require 'date'
(Date.today.strftime('%Y%m%d').to_i - birthday.strftime('%Y%m%d').to_i) / 10_000

ruby(ActiveSupport)

(Date.current.strftime('%Y%m%d').to_i - birthday.strftime('%Y%m%d').to_i) / 10_000
(Date.today.in_time_zone.to_i - birthday.in_time_zone.to_i) / 1.year

javascript

birthDate => Math.floor((new Date() - new Date(birthday).getTime()) / 3.15576e+10);

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
0