Calculating Age in Seconds in Ruby

Understanding the basics of displaying time in programming is pretty important as a lot of programs need to use dates or display the time in some fashion. An easy way to get started learning about how to utilize time is making a calculator that determines someone’s age in seconds. Creating the calculator requires learning several time related concepts such as displaying the current time, calculating the time between two times and using user input as a valid date.

The Time Class

Before you can create a calculator it’s important that you understand the basics of the Ruby Time class. The time object in the Time class is stored by default as the number of seconds that have passed since January 1st 1970, the Unix Epoch date.

To get the current time use the Time.now method, it displays to the screen in the following format: “1960-12-31 00:00:00 -0600”. To get the time at a specific date use the Time.new() method, you can specify the date all the way down to the second but remember that the Time Object uses the 24 hour clock when specifying the hour. You can make the date as specific or general as you like when creating a new one, if you don’t pass any parameters to the method it’ll just return you the current system time.

The Time class has lots of nifty little methods that can tell you specific information about any date that you input. For example the “.tuesday?” method will return true if the day of the week your date falls on was a Tuesday and return false otherwise, similar methods exist for every day of the week. In addition the “.year” method will return the year of the time, “.month” the month, “.day” the day and so on all the way down to the second of the minute.

These are just a few of the dozens of methods that are included in the Time class and more than what you’ll need to calculate someone’s age in seconds.

The Compact Calculator

Once you understand how the time library works creating the age in seconds calculator becomes quite easy. All you have to do is first prompt the user to enter their date of birth and save it into a variable using .gets. Make sure to specify that they must enter the year first as the Time object records dates starting with the year. Also don’t worry about the slashes, the .new method for time treats them like commas automatically so the variable will be interpreted as a year, month, and day being entered.

puts "Enter Your Date of Birth(Ex:1990/10/16)" 
dob=gets.chomp

After this you can simultaneously print the answer to the screen and calculate it by using string interpolation to insert the calculation into the string being printed. The calculation itself is just Time.new being subtracted from the current time, with the user’s input being passed as the argument to Time.new

print "You are #{Time.now-Time.new(dob)} seconds old."

And there you have it, a functioning age in seconds calculator and done in only three lines.

Further Suggestions

The calculator that I’ve made here functions perfectly fine but is only accurate down to the day. For a bit of extra work you could make the calculator accurate to the second and also tell the user what day of the week they were born on, You can also additionally display the user’s age in minutes, hours, days, and months.

Good Luck!

Download the above source code here

If you have any questions or comments email me at nick@crumbsofcode.com