Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Datepicker don't show selected date properly #982

Closed
RodrigoPereyraDiaz opened this issue Feb 15, 2012 · 13 comments
Closed

Datepicker don't show selected date properly #982

RodrigoPereyraDiaz opened this issue Feb 15, 2012 · 13 comments
Milestone

Comments

@RodrigoPereyraDiaz
Copy link
Contributor

In the form of a new object that have a field datetime, the datepicker for complete the field don't show selected date properly.

Ex: if I choose 15/02/2012 in spanish, the field show '15 15e febrero 15e 2012' instead of '15 de febrero de 2012'

When save the object for this example, the persisted value in the database is '15/02/2015' instead of '15/02/2012'

@bbenezech
Copy link
Collaborator

RailsAdmin chokes on 'es.date.formats.long' => '%d de %B de %Y'

Maybe it's the repetition of 'de' that cause the parsing failure.

You should change the key until it gets fixed.

@RodrigoPereyraDiaz
Copy link
Contributor Author

That's right, change the key 'es.date.formats.long' from '%d de %B de %Y' to '%d %B %Y' and now works fine.
Certainly as you say is a parsing problem caused by the repetition of 'de'
I hope is corrected.

@rsierra
Copy link

rsierra commented Apr 9, 2012

The problem is with this function in 'lib/rails_admin/config/fields/type/datetime.rb':

# Ruby to javascript formatting options translator
 def js_date_format
  # Ruby format options as a key and javascript format options
  # as a value
  translations = {
    "%a" => "D",          # The abbreviated weekday name ("Sun")
    "%A" => "DD",         # The  full  weekday  name ("Sunday")
    "%b" => "M",          # The abbreviated month name ("Jan")
    "%B" => "MM",         # The  full  month  name ("January")
    "%d" => "dd",         # Day of the month (01..31)
    "%D" => "mm/dd/y",    # American date format mm/dd/yy
    "%e" => "d",          # Day of the month (1..31)
    "%F" => "yy-mm-dd",   # ISO 8601 date format
    # "%H" => "??",         # Hour of the day, 24-hour clock (00..23)
    # "%I" => "??",         # Hour of the day, 12-hour clock (01..12)
    "%m" => "mm",         # Month of the year (01..12)
    "%-m" => "m",         # Month of the year (1..12)
    # "%M" => "??",         # Minute of the hour (00..59)
    # "%p" => "??",         # Meridian indicator ("AM" or "PM")
    # "%S" => "??",         # Second of the minute (00..60)
    "%Y" => "yy",         # Year with century
    "%y" => "y",          # Year without a century (00..99)
  }
  localized_date_format.gsub(/%\w/) {|match| translations[match]}
end

So with the :es locale, and the long format ('%d de %B de %Y'), this '"%e" => "d"' replace the 'd' in 'de' with the day selected. I can't think now how to fix this, but a better solution than change the long format (maybe you are using it in other places), it could be configure rails admin field to use another format, :default or another custom added in locale files:

For all views

configure :date_field_name, :date do
  date_format :default
end

Or only in the edit views

field :date_field_name do
  date_format :default
end

You can use 'strftime_format "%d/%m/%Y"' too, but I think it's better to set this format in locale files.

Thanks for this awesome engine 😃

@mauriciomdea
Copy link

This happens with pt-br locale too, and rsierra fix resolves it as well.

@AlexEscalante
Copy link
Contributor

I can report that problem too

@dcabo
Copy link

dcabo commented Jan 7, 2015

Same here, with spanish. I fixed by adding the following lines at the end of locales/rails_admin.es.yml:

  date:
    formats:
      default: ! '%d/%m/%Y'
      long: ! '%d %B %Y'
      short: ! '%d %b'

(Note that there are two spaces before date:, since it belongs to the root node es: at the beginning of the file.)

@mshibuya
Copy link
Member

Closed by #2451.

@rodinux
Copy link

rodinux commented Mar 3, 2016

I have the same issue with French translation. I'd try the method of dcabo, but it didn't match as I configured all the application with I18n-fr...
My default format: for datetime is :"%A %d %B %Y %Hh %M"

@rodinux
Copy link

rodinux commented Mar 3, 2016

I try to change, but it doesn't match :

no implicit conversion of nil into String
Extracted source (around line #36):

34 when '%A'
35 english = ::I18n.t('date.day_names', locale: :en)
36 day_names.each_with_index { |d, i| date_string = date_string.gsub(/#{d}/, english[i]) }
37 when '%a'
38 english = ::I18n.t('date.abbr_day_names', locale: :en)
39 abbr_day_names.each_with_index { |d, i| date_string = date_string.gsub(/#{d}/, english[i]) }

@rodinux
Copy link

rodinux commented Mar 3, 2016

Ok, now it matches. But there is no hours in no hours anyway, just because of the function in 'lib/rails_admin/config/fields/type/datetime.rb' ? It is not possible to change this file ?

@rodinux
Copy link

rodinux commented Mar 3, 2016

I found a solution, for my views as I use a calendar which need the hours, I have added in my locales/fr.yml for the translalation

  time:
    am: am
    date:
    formats:
      default: ! '%d/%m/%Y'
      long: ! '%d %B %Y'
      short: ! '%d %b'
      only_day: ! "%e"
      very_long: ! '%A %d %B %Y %Hh %M'
      day_names: [lundi, mardi, mercredi, jeudi, vendredi, samedi, dimanche]
      abbr_day_names: [lun, mar, mer, jeu, ven, sam, dim]
      month_names: [~, janvier, février, mars, avril, mai, juin, juillet, août, septembre, octobre, novembre, décembre]
      abbr_month_names: [~, janv, févr, mars, avr, mai, juin, juill, août, sept, oct, nov, déc]
      order:
        - :day
        - :month
        - :year

And I can use use 'my_field.hour, format: :very_long' to get the date ans the hour

@rodinux
Copy link

rodinux commented Mar 21, 2016

I have a better recommendation... with my code before, updating an object with an hour always put it back to midnight 00:00. But I found how to resolve it...
In my translations I made this below
my fr.yml (the end of this file :

  time:
    am: am
    date:
    formats:
      default: ! '%d/%m/%Y'
      long: ! '%d %B %Y %H:%M %p'
      short: ! '%d %b'
      only_day: ! "%e"
      very_long: ! '%A %d %B %Y %Hh %M'
      day_names: [lundi, mardi, mercredi, jeudi, vendredi, samedi, dimanche]
      abbr_day_names: [lun, mar, mer, jeu, ven, sam, dim]
      month_names: [~, janvier, février, mars, avril, mai, juin, juillet, août, septembre, octobre, novembre, décembre]
      abbr_month_names: [~, janv, févr, mars, avr, mai, juin, juill, août, sept, oct, nov, déc]
      order:
        - :day
        - :month
        - :year
        - :hour  

in my rails_admin.yml :

  date:
    formats:
      default: ! '%d/%m/%Y'
      long: ! '%d %B %Y %H:%M %p'
      short: ! '%d %b'
      only_day: ! "%e"
      very_long: '%A %d %B %Y %Hh %M'
      day_names: [lundi, mardi, mercredi, jeudi, vendredi, samedi, dimanche]
      abbr_day_names: [lun, mar, mer, jeu, ven, sam, dim]
      month_names: [~, janvier, février, mars, avril, mai, juin, juillet, août, septembre, octobre, novembre, décembre]
      abbr_month_names: [~, janv, févr, mars, avr, mai, juin, juill, août, sept, oct, nov, déc]
      order:
        - :day
        - :month
        - :year
        - :hour

and now it seems to be all right, I can edit without breaking the datetime, and change it also in rails_admin...

@zhengjia
Copy link

zhengjia commented Nov 26, 2016

I want to point out that this may apply to other locales too. This is because rails-i18n gem which you may use may specify the long format as long: "%Y年%b%d日". Notice the month field is just a number without translated month name. Then rails_admin uses long format by default for its date and datetime fields (https://github.com/sferik/rails_admin/blob/v1.1.0/lib/rails_admin/config/fields/types/date.rb#L11). As a result you may need to add month_names in your locale file so rails_admin can correctly delocalize it ( https://github.com/sferik/rails_admin/blob/v1.1.0/lib/rails_admin/support/datetime.rb#L31). I updated the wiki in case people run into this again https://github.com/sferik/rails_admin/wiki/Translations#datepicker

Update: another solution in this PR https://github.com/sferik/rails_admin/pull/2788/files

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants