0

I have condition that when I put some value, the jqueryui datepicker shows days and when I put another value, the datepicker just shows months

function setdate(q){
            var d=new Date();
            if (q==1){
                var type={ 
                        0:"dd-mm-yyyy",
                        1:"days", 
                };
                d.setDate(d.getDate()-15);
            }
            else if (q==2){
                var type={ 
                        0:"mm-yyyy",
                        1:"months", 
                };
                d.setDate(d.getDate()-30);  
            }
            viewdate(d,type);
         }

         function viewdate(d,type){
            $('#from').datepicker({
                        format: type[0],
                        startView:type[1], 
                        minViewMode: type[1]
                    });
            $('#from').datepicker( 'setDate',d);
            $('#till').datepicker({
                        format: type[0],
                        startView:type[1], 
                        minViewMode: type[1]
                    });
            $('#till').datepicker( 'setDate', new Date());

         }

But when I put the months value, the datepicker is still showing days.

1 Answer 1

0

The option is not called format but dateFormat (see the docs):

$('#from').datepicker({
    dateFormat: type[0],
    ...

Second if the datepicker was already initialized you need to add the option parameter, else the call has no effect.

$('#from').datepicker('option', {
    dateFormat: type[0],
    ...

Also note that in the format string yy is used to display a four digit year (yyyy would display the year twice).

4
  • hhmmm, still not working. instead the date format is changing become dd/mm/yyyy.
    – sidzaky
    Commented Apr 14, 2016 at 16:34
  • @sidzaky see my edit, probably you need to add the option parameter
    – wero
    Commented Apr 14, 2016 at 16:39
  • same like my first comment.
    – sidzaky
    Commented Apr 14, 2016 at 16:45
  • :UPDATE after couple hours of coding i figure out that the datepicker i'm using is old version. and after i use you'r coding it works. sorry for confusing you @wero
    – sidzaky
    Commented Apr 15, 2016 at 18:05

Not the answer you're looking for? Browse other questions tagged or ask your own question.