administrate icon indicating copy to clipboard operation
administrate copied to clipboard

Global Date/Time Field Timezone

Open mnort9 opened this issue 6 years ago • 4 comments

Is there any way to set a global date field display timezone other than changing the Rails config setting?

I'd rather not make an app-wide timezone change for displaying dates in an admin.

If not, I can create a PR. Any suggestions on where it should be set?

mnort9 avatar Jul 20 '18 19:07 mnort9

@nickcharlton - Any thoughts on this? It's currently resolving to the application time zone (https://github.com/thoughtbot/administrate/pull/1070).

ACPK avatar Aug 21 '19 21:08 ACPK

I think there is a way: generate attribute partials for the Date and DateTime fields and use the Rails helpers to convert the dates into the desired timezone. It's not ideal since you need to duplicate it in up to 6 partials, but it's an option.

dgmstuart avatar Sep 22 '20 16:09 dgmstuart

I'm wondering if the right solution here is to use the timezone of the user who's currently using the dashboard.

Are you providing this as an option? I think that's where I'd start and then from there having Administrate know how to pick that up would be ideal.

nickcharlton avatar Oct 05 '20 18:10 nickcharlton

I think there is a way: generate attribute partials for the Date and DateTime fields and use the Rails helpers to convert the dates into the desired timezone. It's not ideal since you need to duplicate it in up to 6 partials, but it's an option.

I think this is the right approach. Personally I don't mind the form being in a different format as the user can pick a date and time here. So that would leave me with 4 partials. If you use a localization file for your date and time formats you could use one of those formats or create your own.

en.yml

en:
  time:
    formats:
      ...
      long: "%B %d, %Y %H:%M"
      ...

e.g. app/views/fields/date_time/_index.html.erb

<% if field.data %>
  <%= I18n.l(field.data, format: :long) %>
<% end %>

yourivdlans avatar Oct 07 '20 13:10 yourivdlans