datepicker bootsrap中的“TypeError:date.match不是函数”

时间:2015-08-04 04:53:35

标签: javascript jquery arrays datepicker bootstrap-datepicker

当我尝试设置要在datepicker中显示的日期列表时,我收到此错误“”TypeError:date.match不是函数“

$('#datepicker').datepicker({
    format: "dd/mm/yyyy",
    language: "pt-BR",
    multidate: true
});
//this dates is just a example, I already try to create from another way like:
// var data = new Array("2015-06-19", "2015-06-15") 
//and also not work!

var dates = ["2015-06-19", "2015-06-15"];
$('#datepicker').datepicker('update', datas);

我注意到doc“每个日期都被假定为”本地“日期对象,并将转换为UTC供内部使用。”

我不明白这意味着什么。我应该创建日期列表还是数组?它们之间有什么区别,它们不是一回事?

3 个答案:

答案 0 :(得分:1)

A" local" Date对象,指Javascript Date Object

初始化它的一些例子

var today = new Date();
var birthday = new Date('December 17, 1995 03:24:00');
var birthday = new Date('1995-12-17T03:24:00');
var birthday = new Date(1995, 11, 17);
var birthday = new Date(1995, 11, 17, 3, 24, 0);

var unixTimestamp = Date.now(); // in milliseconds

正如在另一个答案中建议的那样,您可以使用setDates

$('#datepicker').datepicker({
  format: "dd/mm/yyyy",
  language: "pt-BR",
  multidate: true,
});

var datas = [
    new Date(2015, 5, 19), //remember, months in javascript starts with 0 for january
    new Date(2015, 5, 15)
];
$('#datepicker').datepicker('setDates', datas)

JSFiddle demo

答案 1 :(得分:1)

问题是更新不会将数组作为其第二个参数。

一个选项是你可以使用setDates

$('#datepicker').datepicker({
  format: "dd/mm/yyyy",
  language: "pt-BR",
  multidate: true,
});

var datas = ["2015-06-19", "2015-06-15"];
$('#datepicker').datepicker('setDates', datas)
<script type="text/javascript" src="//code.jquery.com/jquery-1.10.1.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/css/bootstrap.css" />
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/css/bootstrap-theme.css" />
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/js/bootstrap.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.4.0/css/bootstrap-datepicker3.css" />
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.4.0/js/bootstrap-datepicker.js"></script>

<input id="datepicker" />

或做类似

的事情

$('#datepicker').datepicker({
  format: "dd/mm/yyyy",
  language: "pt-BR",
  multidate: true,
});

var datas = ["2015-06-19", "2015-06-15"];
datas.unshift('update');
var $el = $('#datepicker');
$el.datepicker.apply($el, datas)
<script type="text/javascript" src="//code.jquery.com/jquery-1.10.1.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/css/bootstrap.css" />
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/css/bootstrap-theme.css" />
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/js/bootstrap.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.4.0/css/bootstrap-datepicker3.css" />
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.4.0/js/bootstrap-datepicker.js"></script>

<input id="datepicker" />

答案 2 :(得分:0)

我知道了,我只是改变了最后一行代码:

$('#datepicker').datepicker('update', datas); //to
$('#datepicker').datepicker('setDate', datas); 

全部谢谢!