2014-04-01 30 views
0

我正在使用bootstrap-datepicker.js並遇到問題。我有一個開始日期和結束日期。對於開始日期,我有一些計算來確定本月的第一天(我現在已經設置爲5,因爲它是今天的第一天)。Bootstrap Datepicker預先選擇當月的第一天

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

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

This works。什麼似乎是問題是插入到實際的日曆。它想要選擇現在的哪一天。

我試圖調用此如

$('.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(''); 

的幾種不同的方式和也只是

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


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

任何幫助,將不勝感激!

回答

1

你的問題是你是如何創建新的日期對象。你可以更簡單地使用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); 

這裏是一個工作的jsfiddle:http://jsfiddle.net/JBh3x/3/

+0

我做了一個更新的例子顯示一個與該月的「開始」和一個與一個月與Moment.js的幫助下,最終HTTP:/ /jsfiddle.net/JBh3x/5/ – JoseM

相關問題