Search code examples
ruby-on-railsdatetimetimeformatstrftime

How to correctly set time format in strftime method?


I have the following method

 <%= message.created_at.strftime("%H:%M") %>

that currently returns 21:00

I need to display a Europe/Kiev time zone which is 3 hours ahead than usual Greenwich's time zone. How do I set it in the method input?


Solution

  • If you stored time in your database in UTC than that time will always be in UTC when you fetch it. You need to display it according to user's local time zone. You have to do that using JavaScript, check this article.

    Or if you need just Kiev time zone and nothing else you can do something like this:

    <%= message.created_at.in_time_zone('Europe/Kiev').strftime("%H:%M") %>