0

I added the following line in Gemfile.

gem 'american_date'

Rails is 3.2.13 and ruby is 1.9.3.

Everything's fine except that text field shows yyyy-mm-dd format instead of mm/dd/yyyy.

How do I set it correctly?

Sam

Edit:

I have the following configuration in the initializer.

Date::DATE_FORMATS.merge!(default: "%m/%d/%Y")
Time::DATE_FORMATS.merge!(default: "%m/%d/%Y %H:%M:%S")

However, the text field is not using the format. Why don't know why.

Sam Kong
  • 5,472
  • 8
  • 51
  • 87

2 Answers2

1

Why not just use a custom Time formatter? You don't need a whole gem to output mm/dd/yyyy format for a Time object.

Community
  • 1
  • 1
Rein Henrichs
  • 15,437
  • 1
  • 45
  • 55
0

I solved this problem using jQuery. I put the code inside of ready event.

  $('input.date').each(function() {
    if (/\d\d\d\d-\d\d-\d\d/.test(this.value)) {
      var d = $.datepicker.parseDate("yy-mm-dd",  this.value);
      this.value = $.datepicker.formatDate('mm/dd/yy', d);
    }
  })
Sam Kong
  • 5,472
  • 8
  • 51
  • 87