I have an input with a value dynamically generated
<input name="etd" type"date" value="<?php echo $row[0]; ?>">
To support html5 date input, this value is assigned the yyyy-mm-dd date format, and the mm/dd/yyyy format is displayed correctly in html5 browsers.
The problem occurs in browsers without html5, where the assigned value is displayed as text directly ( yyyy-mm-dd format). My attempt to convert this value to mm/dd/yyyy format using jQuery is as follows:
<script> $('input[type="date"]').each({ var now = new Date($(this).attr('value')); var day = ("0" + now.getDate()).slice(-2); var month = ("0" + (now.getMonth() + 1)).slice(-2); var today = now.getFullYear()+"-"+(month)+"-"+(day); $(this).val(today); }); </script>
but it doesnβt work, I am new to jQuery, so I think something is with it.
UPDATE:
I use this code to host date pickers in browsers that do not support html5, this does not solve my problem with dates retrieved from the database, but the if clause can somehow distinguish between html5 browsers. I think maybe I can use this condition to filter jQuery code (this code I'm looking for)
<script> $(function() { if (!Modernizr.inputtypes['date']) { $('input[type=date]').datepicker({ dateFormat: 'mm/dd/yy' }); } }); </script>
source share