Created
March 17, 2016 21:37
-
-
Save kylekeesling/5de85d4bff57cefbf464 to your computer and use it in GitHub Desktop.
Birthday Calculator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
puts "What year where you born? (Enter a 4 digit year, ie 1984)" | |
birth_year = gets.chomp | |
puts "What month where you born? (Enter the month number, ie June would be 6)" | |
birth_month = gets.chomp | |
puts "What day of the month where you born?" | |
birth_day = gets.chomp | |
birth_date = Time.new(birth_year, birth_month, birth_day) | |
# Calculate the time difference between now and the birth date. This gives us the difference in SECONDS. | |
seconds_diff = Time.now - birth_date | |
# Let's figure out how many seconds are in a minute, hour, day, and year. | |
minute_seconds = 60 | |
hour_seconds = 60 * minute_seconds # 60 minutes in an hour | |
day_seconds = 24 * hour_seconds # 24 hours in a day | |
year_seconds = 365 * day_seconds # 365 days in a year (don't worry about leap year.) | |
# Now, divide our age in seconds by each unit to figure out how old we are in those units. | |
years_old = seconds_diff / year_seconds | |
days_old = seconds_diff / day_seconds | |
hours_old = seconds_diff / hour_seconds | |
minutes_old = seconds_diff / minute_seconds | |
puts "Your age in: " | |
puts " Years: #{years_old.round(1)}" | |
puts " Days: #{days_old.round(1)}" | |
puts " Hours: #{hours_old.round(1)}" | |
puts " Minutes: #{minutes_old.round(1)}" | |
# Let's figure out how old we are right now. | |
age_years = years_old | |
# Divide our age in years by 365 to get days. The remainder is how many days it's been since our last birthday. | |
age_days = days_old % 365 | |
age_hours = hours_old % 24 | |
age_minutes = minutes_old % 60 | |
age_seconds = seconds_diff % 60 | |
# To drop the decimal numbers, just convert the number to an integer using .to_i | |
exact_age_sentence = "You are #{age_years.to_i} years, #{age_days.to_i} days" | |
exact_age_sentence = "#{exact_age_sentence}, #{age_hours.to_i} hours, #{age_minutes.to_i} minutes" | |
exact_age_sentence = "#{exact_age_sentence}, #{age_seconds.to_i} seconds old." | |
puts "\n#{exact_age_sentence}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment