find the current date and time in ruby

date, time, ruby, datetime, programming

To find the current date and time in Ruby, you can use the Time class

and call the now method which will return a Time object representing

the current date and time.

current_time = Time.now puts current_time

=> 2022-09-15 16:18:34 +0000

If you want to display the date and time in a specific format, you can use

the strftime method and specify the formatting string.

puts current_time.strftime("%Y-%m-%d %H:%M:%S %Z")

=> 2022-09-15 16:18:34 UTC

The above code will display the date and time in the format "YYYY-MM-DD HH:MM:SS <timezone>".

The %Y, %m, %d, %H, %M, and %S are placeholders for year, month, day, hour,

minute, and second respectively. The %Z placeholder is for the timezone abbreviation.

main.rb
0 chars
1 lines

related categories

gistlibby LogSnag