Skip to main content

API

General • Asked by Jacob Hedengren

Hi everyone,

I have recently set on a mission to learn more about APIs. I get some data from a external JSON API but have come a cross an issue that I need some help with.

I’ve defined my cars variable in my controller and set it to

def index 
    @cars = HTTParty.get('...
end

I am then looping through each car in my view with

<% @cars.each do |car| %>
    <%= car[“date”] %>
<% end %>

The result in my view;

2018-07-13T00:27:03-07:00

How can I change this format to instead say time_ago_in_words?

Thanks alot in advance!

/ Jacob


This will do it.

distance_of_time_in_words_to_now(DateTime.parse(car['date']))
#=> "1 day, 8 hours, and 32 minutes"

Thanks John! Worked perfectly!


Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 27,623+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.