2

I have found a possible bug: datepicker resets initial date when using different date format like "mm yy" or "yy". So, if a select a date, the second time a do this the date is not saved and today's date is preselected.

Sample code:

  <script>
     $(function() {
       $( "#datepicker" ).datepicker({ dateFormat: "mm yy" });
     });
  </script>

  <p>Date: <input type="text" id="datepicker" /></p>

See fiddle example.

It only happens if your format does not contain a "dd" or similar for day values.

Any idea of how to avoid it?

J punto Marcos
  • 437
  • 1
  • 6
  • 24

2 Answers2

0

You should keep all the necessary element of date in your date picker's format.

format: 'dd-mm-yy',
-1

I just got this from this answer to another question; it will solve your problem:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.js"></script>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js"></script>
    <link rel="stylesheet" type="text/css" media="screen" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/themes/base/jquery-ui.css">
<script type="text/javascript">
$(function() {
    $('.date-picker').datepicker( {
        changeMonth: true,
        changeYear: true,
        showButtonPanel: true,
        dateFormat: 'MM yy',
        onClose: function(dateText, inst) { 
            var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val();
            var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val();
            $(this).datepicker('setDate', new Date(year, month, 1));
        }
    });
});
</script>
<style>
.ui-datepicker-calendar {
    display: none;
    }
</style>
</head>
<body>
    <label for="startDate">Date :</label>
    <input name="startDate" id="startDate" class="date-picker" />
</body>
</html>

Reference from: http://jsfiddle.net/bopperben/DBpJe/

Community
  • 1
  • 1
Mike Phils
  • 3,475
  • 5
  • 24
  • 45