Skip to content
Advertisement

Parsing Time field from Postgres db in rails 6 to retrieve hour and minute?

I have a model Open times for shops. The open times tables comprises a field as below. I would like to query the opening and closing times like: 08:00 – 17:00

the view helpers is the following: <%= display_day(0) %>. where 0 is sunday

I get to the following array with pluck.

I would like to ignore the date component and only show Hours and Minutes.

Thanks to tadman and Caleb, we could get to the following:

I will clean the code and post it there. in the meantime, if you have any suggestions, feel free to suggest.

Advertisement

Answer

You should be able to use both the DateTime parse and strftime methods to format the time however you want.

Based on the plucking you’re doing:

You could create a little formatting helper like:

And then use this to return the final string like: 08.00 - 17.00:

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement