javascript 日期选择器引导程序中的“TypeError:date.match 不是函数”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31800945/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"TypeError: date.match is not a function" in datepicker bootsrap
提问by Rodrigo de Farias
I got this error ""TypeError: date.match is not a function" when i try to set up a date list to be showed in datepicker
当我尝试设置要在 datepicker 中显示的日期列表时,出现此错误“"TypeError: date.match is not a function"
$('#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);
I notice in the doc"Each date is assumed to be a “local” date object, and will be converted to UTC for internal use."
我在文档中注意到“每个日期都假定为“本地”日期对象,并将转换为 UTC 以供内部使用。”
I don't understood what it means. Should I create a list of dates or an Array? what is the difference between them, they are not the same thing?
我不明白这是什么意思。我应该创建日期列表还是数组?它们之间有什么区别,它们不是一回事?
采纳答案by Rodrigo de Farias
I got it, I just change the last row of code:
我明白了,我只是更改了最后一行代码:
$('#datepicker').datepicker('update', datas); //to
$('#datepicker').datepicker('setDate', datas);
Thanks All!
谢谢大家!
回答by davcs86
A "local" Date
object, refers to Javascript Date Object
“本地”Date
对象,指的是Javascript 日期对象
Some examples of how initialize it
如何初始化它的一些例子
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
As it's suggested in another answer, you can use setDates
正如另一个答案中所建议的那样,您可以使用 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)
回答by Arun P Johny
The problem is update won't take an array as its second parameter.
问题是 update 不会将数组作为其第二个参数。
One option is you can use setDates
一种选择是您可以使用 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" />
Or do something like
或者做类似的事情
$('#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" />