Ruby on Rails 6 — Format a date with the “th” suffix, e.g. ‘30th April 2020’

Sergii Chub
1 min readMay 1, 2020
# Returns formatted date, e.g. '30th April 2020'
def
formatted_timestamp(date)
parsed_date = DateTime.parse(date)
parsed_date.strftime("#{parsed_date.day.ordinalize} %B %Y")
end

spec

describe '.formatted_timestamp' do
subject
(:method) { helper.formatted_timestamp(date) }

let(:date) { '2020-04-30T08:08:31' }

it 'returns a proper date format' do
expect(method).to eq('30th April 2020')
end
end

Source

--

--