w3resource

Ruby Basic Exercises: Display the current date and time

Ruby Basic: Exercise-2 with Solution

Write a Ruby program to display the current date and time.

Ruby Basic Exercises: Display the current date and time

Ruby Code:

require 'date'
current_time = DateTime.now
cdt = current_time.strftime "%d/%m/%Y %H:%M"
puts "Current Date and Time: "+cdt

Output:

Current Date and Time: 27/12/2017 10:30

Flowchart:

Flowchart: Display the current date and time

Ruby Code Editor:

Contribute your code and comments through Disqus.

Contribute your code and comments through Disqus.

Previous: Write Ruby program to get ruby version with patch number.
Next: Write a Ruby program to create a new string which is n copies of a given string where n is a non-negative integer.

What is the difficulty level of this exercise?