0

I have a scenario with jQuery datepicker where the user is editing an event that occurs in September, but the current month is June.

The user unselects all dates and the datepicker but the datepicker jumps back to June.

The required behaviour is that the datepicker stays in September, with no dates selected.

Is there a default parameter to handle this, without writing a custom handler in the onSelect hook?

blarg
  • 3,773
  • 11
  • 42
  • 71
  • https://stackoverflow.com/a/40491012/4229270 or set parameter as `defaultDate: '01-01-2018'` – Sinto Jun 12 '18 at 10:02
  • @Sinto that solution is for when no dates are initally selected. This scenario is different in that dates are already selected, but on unselect the default date should be set in the calendar without selecting a date. – blarg Jun 12 '18 at 10:04
  • I do not think there is default month setting parameter as `defaultDate` – Sinto Jun 12 '18 at 10:08
  • @Sinto, defaultDate is what I was looking for, thanks. Please add this an answer so I can accept. – blarg Jun 12 '18 at 10:13

1 Answers1

1

There is no datepicker parameter to set a default month, you have to set it by default date option. Helpful link here

defaultDate

Type: Date or Number or String

Default: null

Set the date to highlight on first opening if the field is blank. Specify either an actual date via a Date object or as a string in the current dateFormat, or a number of days from today (e.g. +7) or a string of values and periods ('y' for years, 'm' for months, 'w' for weeks, 'd' for days, e.g. '+1m +7d'), or null for today.

Multiple types supported:

  • Date: A date object containing the default date.
  • Number: A number of days from today. For example 2 represents two days from today and -1 represents yesterday.
  • String: A string in the format defined by the dateFormat option, or a relative date. Relative dates must contain value and period pairs; valid periods are "y" for years, "m" for months, "w" for weeks, and "d" for days. For example, "+1m +7d" represents one month and seven days from today.

Code examples:

Initialize the datepicker with the defaultDate option specified:

$( ".selector" ).datepicker({
    defaultDate: +7
});

OR

$( ".selector" ).datepicker({ defaultDate: new Date() });

OR

$(function() {
  $('.selector').datepicker( {
    // ...
  });

  var default_date = new Date(2023, 10, 1);
  $(".selector").datepicker("setDate", default_date);
});
Sinto
  • 3,915
  • 11
  • 36
  • 70