Date Format with datepicker revisited

112 views
Skip to first unread message

Thomas Brooke

unread,
May 19, 2013, 10:50:27 AM5/19/13
to plataformate...@googlegroups.com

I am revisiting this discussion: format date :as => :string

I am using DateEntry as a date picker that produces string input similar to datepicker

I defined a new input type:

    class DatePickerInput < SimpleForm::Inputs::StringInput
      def input
        value = object.send(attribute_name) if object.respond_to? attribute_name
        input_html_options[:value] ||= I18n.localize(value) if value.present?
        input_html_classes << "datepicker"

        super # leave StringInput do the real rendering
      end
   end

   My form looks like this:

      = f.input :birth_date, as: :date_picker, input_html: { id: 'dateEntry', class: 'span2 datepicker' }

My javascript is 

     $('#dateEntry').datetimeEntry({datetimeFormat: 'O/D/Y'});

I have set my en.yml and simple_form.en.yml to:

    date:
        formats:
        default: '%m/%d/%Y'

but When I enter 02/09/1952 - I should get Feb 9, 1952  but instead I get Sept 2, 1952

Also when I revisit the form I get Tues, 02 sept 1952 00   instead of 02/09/1952

I am lost as to the date format issue - I am thinking that as to the formatting on revisit it is because the string conversion doesn't apply when reading from the database  

Any ideas about fixing this



 

Steve Nelson

unread,
Jun 28, 2013, 10:32:09 AM6/28/13
to plataformate...@googlegroups.com
I'm in a very similar predicament and it's making me nuts!

I'm making a scheduling app for my small photo business.
The time/date picker that best suits my needs is Trent Richardson's Timepicker jQuery datepicker addon.
I started with this in my SimpleForm:

<%= f.input :starts_at, :as => :string %>

and this in my coffeescript:

dateFormat: 'yy-mm-dd',
timeFormat: 'hh:mm tt'

This works fine to create a new record.
However, if I go to edit that record, the time part of the datetime picker doesn't display the previously set time, displaying instead the plugin's default.

Trent suggested this was because the time date field in the existing record needs to match what the jQuery module expects to see in terms of formatting.

I modified the SimpleForm input as follows:
<%= f.input :starts_at, :as => :string, :input_html => { :value => localize(f.object.starts_at, :format => "%m-%d-%Y %I:%M %P") } %>

This works perfectly for an existing record but throws an error when creating a new record since there is no existing :value to parse.

I tried the following (and variations of the following) with no success:
<%= f.input :starts_at, :as => :string, :input_html => { :value => localize(f.object.starts_at, :format => "%m-%d-%Y %I:%M %P") if :value.present? } %>

I next tried creating a custom SimpleForm input, apparently stumbling across the same source Thomas did, but, similar to Thomas' experience, couldn't manage to have this format the text input field, even after putting the following in the en.yml and simple_form.en.ymf locales files:

datetime:
  formats:
    default: ! '%Y-%m-%d %I:%M %P'

I would be SO grateful for any pointers as to how to conditionally format a string input field if it has content.

Steve

Steve Nelson

unread,
Jul 4, 2013, 3:25:39 PM7/4/13
to plataformate...@googlegroups.com
Cautiously optimistic. This seems to be working (without any custom input type) for my situation:

<% if @assignment.new_record? %>
    <%= f.input :starts_at, :as => :string %>
    <% else %>
    <%= f.input :starts_at, :as => :string, :input_html => { :value => localize(f.object.starts_at, :format => "%Y-%m-%d %I:%M %P") } %>
 <% end %>

Again, with this in the assignments.js.coffee (because it has to match for the jQuery plugin to work):

dateFormat: 'yy-mm-dd',
timeFormat: 'hh:mm tt'

I had been trying to put the condition inline with the f.input statement which failed, and then testing for a value in the :starts_at field, which likewise failed.
Testing for whether this is a new record or not seems to be working. For my situation anyway.
Reply all
Reply to author
Forward
0 new messages