Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display local time in view

In config/application.rb I have "config.time_zone = 'UTC'" (without quotes) in the file. I am assuming this is to make the conversion from the user's time, which is entered into the view, to UTC, which is stored in the database. My question is, how do I convert the UTC value from the database to the user's local time to display in the view? I have read that rails takes care of that automatically - how do I tell it to do this?

I have a timezone field in each user's row in the database, I'm just not sure what to store in there, also. I know about rake time:zones:all - I just don't know how all of this fits together in rails 3!

Thank you,

sk

like image 340
dingalingchickenwiing Avatar asked Sep 25 '10 10:09

dingalingchickenwiing


People also ask

How do I display the time in a different time zone?

If you have a server in a different time zone than your customers, you probably want to display the time in their local zone. The only correct way to achieve this is to provide the localization formatting on the client. Javascript provides two functions to format a date object to the local date and time string.

How to display the local time in query editor?

Powerbi Service now only supports utc time, if you want to display the local time, you can create a custom column in Query Editor use DateTime.LocalNow () and #duration () function. If this post helps, then please consider Accept it as the solution to help the other members find it. 04-15-2021 05:40 AM Thanks for confirming. 04-12-2021 11:19 AM

How to display the local time in Power BI service?

Powerbi Service now only supports utc time, if you want to display the local time, you can create a custom column in Query Editor use DateTime.LocalNow () and #duration () function. If this post helps, then please consider Accept it as the solution to help the other members find it.

How to display the date and time in HTML?

The easiest way to do that is to create a display template. The great thing with that is that all date/times will automatically be reformatted if you are using Html.DisplayFor. Create a template named /Views/Shared/DisplayTemplates/DateTime.cshtml with the following content: We simply display the date as UTC.


1 Answers

When working in multi-zone environment it's wise to have time zone set to UTC. That's perfectly valid in your application.rb

Rails will automatically transform all time to the current time zone, that can be set with

Time.zone = "some-zone"

What I use is a before_filter in ApplicationController where I set the time zone according to the current user. Then all operations works within this zone and you don't need to think about it in your controllers/models/views.

Suppose that you have some model Foo with some datetime field. Then working in the irb console:

Time.zone = "Prague"
x = Foo.create(:it_will_happen_at => Time.zone.now)
x.it_will_happen_at # => Sat, 25 Sep 2010 13:45:46 CEST +02:00 

Time.zone = "London"
# it is needed to refresh the field after a time zone has changed.
# In normal situation it'd not be needed, it's just for this console example
x.reload
x.it_will_happen_at # => Sat, 25 Sep 2010 12:44:46 BST +01:00 

When you take a look into the DB, you'll find that value is Sat, 25 Sep 2010 11:45:46 UTC.

As for the zone value I prefer names of cities as it works smoothly with daylight savings (summer / winter time).

like image 171
Radek Paviensky Avatar answered Sep 21 '22 05:09

Radek Paviensky