blarg
blarg

Reputation: 3893

How to set a default month for datepicker when unselecting all dates?

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?

Upvotes: 0

Views: 3135

Answers (1)

Sinto
Sinto

Reputation: 3997

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);
});

Upvotes: 1

Related Questions