Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As you'll have noticed, supplying a dateFormat works well for <em>newly entered dates</em>, but it does not alter the <code>value</code> attribute which was already supplied to the date input field. It took me some time and I'm not sure whether this solution is ideal, but here's my situation explained and the code which solves it. Might help others with the same problem in the future. In my example I'm using <code>dd/MM/yyyy</code> as the display format.</p> <ol> <li>The page contains any number of date input fields, which may or may not already have a <code>value</code> attribute supplied in the format <code>yyyy-MM-dd</code>, as <a href="https://www.w3.org/TR/html5/forms.html#date-state-(type=date)" rel="nofollow noreferrer">specified by W3C</a>.</li> <li>Some browsers will have their own input control to handle dates. At the time of writing, that is for instance Opera and Chrome. These should expect <em>and store</em> a date in the abovementioned format, while rendering them according to the client's regional settings. You probably do not want/need to create a jqueryui datepicker in these browsers.</li> <li>Browsers which don't have a built-in control to handle date input fields will need the jqueryui datepicker along with an 'alt', invisible field.</li> <li>The invisible, alt input field with the <code>yyyy-MM-dd</code> format must have the original name and a unique id in order for forms logic to keep working.</li> <li>Finally, the <code>yyyy-MM-dd</code> value of the display input field must be parsed and replaced with its desired counterpart.</li> </ol> <p>So, here's the code, using <a href="http://modernizr.com/" rel="nofollow noreferrer">Modernizr</a> to detect whether or not the client is able to natively render date input fields. </p> <pre class="lang-js prettyprint-override"><code>if (!Modernizr.inputtypes.date) { $('input[type=date]').each(function (index, element) { /* Create a hidden clone, which will contain the actual value */ var clone = $(this).clone(); clone.insertAfter(this); clone.hide(); /* Rename the original field, used to contain the display value */ $(this).attr('id', $(this).attr('id') + '-display'); $(this).attr('name', $(this).attr('name') + '-display'); /* Create the datepicker with the desired display format and alt field */ $(this).datepicker({ dateFormat: "dd/mm/yy", altField: "#" + clone.attr("id"), altFormat: "yy-mm-dd" }); /* Finally, parse the value and change it to the display format */ if ($(this).attr('value')) { var date = $.datepicker.parseDate("yy-mm-dd", $(this).attr('value')); $(this).attr('value', $.datepicker.formatDate("dd/mm/yy", date)); } }); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload