zazvorniki
zazvorniki

Reputation: 3602

Bootstrap Datepicker preselect first day of the current month

I'm working with the bootstrap-datepicker.js and have run into an issue. I have a start date and an end date. For the start date I have some calculations to determine the first day of the current month (I have it set to five right now for testing because it is the first today).

var d = new Date();
var currDate = d.getDate();
var currMonth = d.getMonth()+1;
var currYear = d.getFullYear();

var firstMonth = currYear + "-" + 5 + "-" + currMonth;

This works. What seems to be the issue is plugging it into the actual calendar. It wants to select what day it is now.

I've tried several different ways of calling this such as

$('.datepicker').datepicker('setDate', new Date(firstMonth));
$('.datepicker').datepicker('update');
$('.datepicker').val('');

$('.datepicker').datepicker('setDate', firstMonth);
$('.datepicker').datepicker('update');
$('.datepicker').val('');


$('.datepicker').datepicker('startdate', firstMonth);
$('.datepicker').datepicker('update');
$('.datepicker').val('');

and the also just

$('.datepicker').datepicker('setDate', new Date(firstMonth));


$('.datepicker').datepicker('setDate', firstMonth);

Any help would be appreciated!

Upvotes: 1

Views: 8821

Answers (1)

JoseM
JoseM

Reputation: 4302

Your issue is how are you are trying to create the new Date object. You can more simply just use the date constructor that accepts the year, month day values.

var firstDayOfMonth = function() {
    // your special logic...
    return 5;
};

var d = new Date();
var currMonth = d.getMonth();
var currYear = d.getFullYear();
var startDate = new Date(currYear,currMonth,firstDayOfMonth());
$('#exampleInputDate').datepicker('setDate',startDate);

Here is a working jsfiddle: http://jsfiddle.net/JBh3x/3/

Upvotes: 1

Related Questions