How to convert only the date without time to a string in Ruby

1 Answer

0 votes
require "date"

# Convert a Date to a string (YYYY-MM-DD)
def date_to_string(d)
  d.strftime("%Y-%m-%d")
end

# Build a Date from integers
def make_date(y, m, d)
  Date.new(y, m, d)
end

# 1. Today's date
today = Date.today
puts "Today's date is: #{date_to_string(today)}"

# 2. Hard-coded date
my_date = make_date(2025, 12, 7)
puts "Hard-coded date is: #{date_to_string(my_date)}"



#
# run:
#
# Today's date is: 2026-05-31
# Hard-coded date is: 2025-12-07
#

 



answered 3 hours ago by avibootz

Related questions

...