user2360274
user2360274

Reputation:

Jquery altFormat datepicker issue in rails app

I have a form with a date input

<div class="form-inputs">
<%= f.input :day, as: :string, input_html: {id: 'date'} %>

And i'm using a datepicker jquery object that I make at the bottom of the page

<script>$(function(){$("#date").datepicker();});</script>

When I have this, everything works, and the datepicker displays as it should, but it displays as month date year, whereas the db and rails takes date month year. So some work when they should not ie (05/07/2013) and then obviously when the date is over 12, it rejects it saying that the field is blank.

I have tried

<script>$(function(){$("#date").datepicker({ dateFormat: "dd-mm-yy",altFormat: "ddmmyy"});</script>

<script>$(function(){$("#date").datepicker({ altFormat: "dd-mm-yy" });</script>

And none of them have worked, am I missing something or do I need to change something somewhere else?

Upvotes: 2

Views: 422

Answers (1)

mu is too short
mu is too short

Reputation: 434615

The examples in the docs are little incomplete. The altFormat option is only used if you also specify the altField:

altFormat
The dateFormat to be used for the altField option.

and:

altField An input element that is to be updated with the selected date from the datepicker. Use the altFormat option to change the format of the date within this field.

The corresponding examples only show each option being used alone but you need to specify altField if you want to use altFormat. The usual approach would be to use a hidden input as the actual date that gets sent to the server and then hook up the user-visible date selector to the hidden input using altField with something like this:

$('#date').datepicker({
    dateFormat: 'dd-mm-yy',
    altField: '#some-hidden-field',
    altFormat: 'yy-mm-dd',
});

Demo: http://jsfiddle.net/ambiguous/jWT5G/

Upvotes: 1

Related Questions