Search code examples
ruby-on-railsrubytwitter-bootstrapbootstrap-datepicker

Set and Display Dates in rails via format: %m/%d/%Y


If you want to save a date attribute in a rails form via a text_field: by default rails wants you to put the date in the format of: %Y-%m-%d. ex: '2016-8-25'.

  • I want to put the date in the format of %m/%d/%Y. ex: '8/25/2016'.
  • When editing this date attribute: I want it to also display the date in that same format.

I have this specified in my config/locals/en.yml file but it is not working:

en:
  date:
    formats:
      default: "%m/%d/%Y"

If there is another strategy different from above: let me know.

Ultimately: all my date attributes application wide use the bootstrap-datepicker-rails gem. Within forms: this gem wants dates to be in the above format, and it makes sense for dates to be in that format anyways. I do not want to have to override all the writer attributes in order for rails to understand this format.


Update

There appears to be some confusion about my stack overflow question. Users are not actually manually entering dates. They are using a datepicker. And when the user picks a date: it saves the date in a textbox with the format of: mm/dd/yyyy. This is how the clients of this small, in-house app want to display the date (and it makes sense that they want the date displayed this way because they live in the United States and this is a typical date format there).

Ultimately here is the scenario:

Here is the relevant input field in the form:

<div class="field">
  <%= f.label :some_date %><br>
  <%= f.text_field :some_date, data: {provide: 'datepicker'} %>
</div>

If you manually enter the string: 2016-1-20, and then submit that:

  • Rails knows how to take that string of 2016-1-20, convert it into a date, and then save that date into the database.
  • Rails also knows how to redisplay that date within the format of 2016-1-20 if the user wants to go back and edit that date.

I want the exact same setup. The only thing I want to change is the format. Change the format of yyyy-mm-dd to mm/dd/yyyy. So the user would pick a date, and into the input field the value 1/20/2016 would go. That would get submited.

  • Rails would know how to convert that to a date and save it to the database.
  • If the user wants to edit this date in the future: it would redisplay in the text field the date in the format of 1/20/2016.

Hopefully this clears up any confusion.


Solution

  • The best solution I have found is a two-step process:

    1. Allow dates to save within the format of mm/dd/yyyy

      Simply add in the american_date gem. And with that: when you enter into a text_field for a date the format of: mm/dd/yyyy (ex: 1/30/2016) it will save the correct date! This works wonderfully with the bootstrap-datepicker-rails gem because when you use the datepicker to pick a date: it loads into the textbox the date in this format.

    2. Display dates in the format of mm/dd/yyyy

      however, even with the american_date gem installed: rails still wants to display the date in the yyyy-mm-dd format.

      Example: within a text_field, such as: <%= f.text_field :date %>, when the user goes back to edit that date, it will redisplay the date in the format of yyyy-mm-dd which is not what we want. We want rails to redisplay the date in the mm/dd/yyyy format.

      Another Example: if you have an erb tag, such as <%= @blog.some_date %> then yet again: rails will display the date in the format of yyyy-mm-dd. Again: this is not what we want. We want that erb tag to automatically redisplay the date in the format of mm/dd/yyyy

      To fix this: you have two options:

      One option is to open up your config/locals/en.yml file and use the setup specified in the original question of this post. I personally do not like this setup because you then need to remember to use the l method everytime you want to display a date within your app.

      The other option (which I like best) is to go to your config/initializers directory and make a new file. Name the file anything you want. I named the file date_display_format.rb. Then within there just put this line: Date::DATE_FORMATS[:default] = "%m/%d/%Y". Now: a date within your erb tags will display in the proper format, and when you go to edit your date attribute within a form: with the text box the date will display in the proper format as well.