Skip to content

Time adding program in ruby

February 10, 2011

The Below coding is used to add the two time


print "Enter the Time (H:M:S)"

time=gets.chomp

print "Enter Time to add the Above time (H:M:S)"

add_time=gets.chomp

time=time.split(':')

add_time=add_time.split(':')

seconds_add = time[2].to_i + add_time[2].to_i

minutes_add = time[1].to_i + add_time[1].to_i

hours_add   = time[0].to_i + add_time[0].to_i

if seconds_add >= 60

minutes_add = minutes_add + 1

seconds_add = seconds_add % 60

end

if minutes_add >= 60

hours_add = hours_add + 1

minutes_add = minutes_add % 60

end

if  hours_add >=12

hours_add = hours_add %12

end

puts hours_add.to_s + ":" + minutes_add.to_s + ":" + seconds_add.to_s

Advertisements
No comments yet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: