Arman H
Arman H

Reputation: 5618

Set month view for `bootstrap-datepicker` without setting date

Is there a way to set the current view of the calendar to a specific month/year, without setting a date?

My use case for this is when a user selects a date, I save this date, and clear the calendar by calling clearDates() on it. However, if the selected date fell on a month in the future, (for example, we're in November, and the selected month is next March), then the calendar's view resets back to the current month (November).

I want to set the calendar's view to the last selected month (March), without setting the date, so that the calendar has no date selected, but is showing the days for March of next year (to allow the user to select another date in the last used month, instead of having to click Next bunch of times to get to March again).

Upvotes: 3

Views: 3630

Answers (1)

acrobat
acrobat

Reputation: 2259

You could use the defaultViewDate option for this behavior

$('#datepicker').datepicker({
    defaultViewDate: {
        year: 2016,
        month: 2
    }
});

The months int is used like the javascript date month. So january = 0 and december = 11

See example here: http://jsfiddle.net/zcvq0yhh/4/

If you want to change it on the fly there is no built-in method (you can create a feature request here)

But this is an workaround to do it: http://jsfiddle.net/zcvq0yhh/5/

Upvotes: 2

Related Questions