javascript - Jquery UI datepicker setDate not working on dynamic change of formatDate -
i'm trying add button in control pane of datepicker. should allow show name of whole month, instead of specific date.
i need both behaviors (date picker , month picker) seems dynamic change of date format crashes widget. commented code has same problem.
var datetoday = new date(); var maxdate = "+365"; $('.datepicker').datepicker({ mindate: datetoday, maxdate: maxdate, dateformat: "dd-mm-yy", changemonth: true, changeyear: true, showbuttonpanel: true, onchangemonthyear: function(year, month, instance) { var thiscalendar = $(this); settimeout(function() { var buttonpane = $(instance) .datepicker("widget") .find(".ui-datepicker-buttonpane"); $("<button>", { text: "whole month", click: function() { var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val(); var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val(); thiscalendar.datepicker("option", "dateformat", "mm yy"); thiscalendar.datepicker('setdate', month + "/01/" + year); //thiscalendar.datepicker('setdate', new date(year, month, 1)); //thiscalendar.datepicker("refresh"); } }).appendto(buttonpane).addclass("ui-datepicker-clear ui-state-default ui-priority-primary ui-corner-all"); }, 1); } }) this fiddle shows issue: https://jsfiddle.net/mlewc6ep/3/. button appears on next months, not current one. if click on it, shows same month (e.g. august 2021, of today).
it looks datepicker bug, messes date when change dateformat , have mindate , maxdate set @ same time.
the solution remove min/max date options, set new format , restore min/max:
thiscalendar.datepicker("option", "mindate", null); thiscalendar.datepicker("option", "maxdate", null); thiscalendar.datepicker("option", "dateformat", "mm yy"); thiscalendar.datepicker("option", "mindate", datetoday); thiscalendar.datepicker("option", "maxdate", maxdate); // hack trick datepicker , keep date set thiscalendar.datepicker("option", "defaultdate", new date(year, month, 1)); thiscalendar.datepicker('setdate', new date(year, month, 1)); here fiddle.
update:
this solution works, if date: thiscalendar.datepicker('getdate'); shows today date. may bug?
in general, not sure if bug or normal behavior. looking @ datepicker code, clear did not expect partial date formats (such year + month, without day).
datepicker doesn't keep date value date, parses text value of input, looks this:
- we set
mm yyformat - we choose date
- datepicker converts
dateobject value textmarch 2016 - for ok, backward process triggered
- datepicker parses
march 2016text , here things go wrong, not able parse , resets valuedefaultdate(which today default)
here place parse date , here place fails, after parsing has year , month value, default value day -1, can not construct date.
the workaround set defaultdate value same value date, when fails parse text, falls value need. updated fiddle , code above.
Comments
Post a Comment