I have an input with a value dynamically generated
<input name="etd" type"date" value="<?php echo $row[0]; ?>">
In order to support html5 date input, a date format yyyy-mm-dd
is assigned to that value, and mm/dd/yyyy
format is shown in html5 browsers correctly.
The problem arises on non html5 browsers where the value assigned is shown as text directly (format yyyy-mm-dd
). My attempt to transform this value to format mm/dd/yyyy
with jQuery is the following:
<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 is not working, I'm new to jQuery, so I think, it's something with it.
UPDATE:
I'm using this code to put a date picker on non html5 capable browsers, this not solves my problem with the dates retrieved from database, but the if clause somehow can difference html5 browsers. I thinking maybe I can use that condition to filter the jQuery code (that code which i'm looking for)
<script>
$(function() {
if (!Modernizr.inputtypes['date']) {
$('input[type=date]').datepicker({
dateFormat: 'mm/dd/yy'
});
}
});
</script>