JQuery Datepicker 返回的 Date 对象类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/154427/
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
JQuery Datepicker returned Date object type
提问by Elliot Vargas
What's the object type returned by Datepicker? Supposing I have the following:
Datepicker 返回的对象类型是什么?假设我有以下内容:
$("#txtbox").datepicker({
onClose: function(date){
//something
}
});
What is date
? I'm interested in reading the date object from another Datepicker for comparison, something like:
什么是date
?我有兴趣从另一个 Datepicker 读取日期对象进行比较,例如:
function(date){
oDate = $("#oDP").datepicker("getDate");
if(oDate == date)
//do one
else if(oDate > date)
//do two
}
However, this kind of comparison is not working. I'm guessing there is some sort of comparison method for Date object, but I don't know. I also tried comparing the String representation of the dates like oDate.toString() > date.toString()
to no avail.
然而,这种比较是行不通的。我猜 Date 对象有某种比较方法,但我不知道。我也尝试比较日期的字符串表示,但oDate.toString() > date.toString()
无济于事。
回答by Pat
I just downloaded the source from hereand noticed (ex line 600) the author is using .getTime() to compare dates, have you tried that?
我刚刚从这里下载了源代码并注意到(前 600 行)作者正在使用 .getTime() 来比较日期,您尝试过吗?
if (oDate.getTime() > date.getTime()) {
...
}
Also this is tangential but you mention you tried oDate.toString() while I noticed in the examplesthe author is using .asString()
这也是切线,但你提到你尝试过 oDate.toString() 而我在例子中注意到作者正在使用 .asString()
回答by ConroyP
A Date
object is returned by the datePicker
.
甲Date
目的通过返回datePicker
。
Your method for comparing dates is valid - from W3schools:
您比较日期的方法是有效的 - 来自W3schools:
var myDate=new Date();
myDate.setFullYear(2010,0,14);
var today = new Date();
if (myDate>today)
{
alert("Today is before 14th January 2010");
}
Are you getting a value in oDate
from this line?
你oDate
从这条线得到了价值吗?
oDate = $("#oDP").datepicker("getDate");
Your comparison method seems valid - so I'm wondering if datePicker
is successfully pulling a value from #oDP
?
您的比较方法似乎有效 - 所以我想知道是否datePicker
成功地从#oDP
?
Edit- oDate
confirmed to contain a valid date. This may be a very silly question, but have you confirmed that date
contains a valid date? I'm wondering if there may be some issue with naming it the same as the keyword Date
(Javascript keywords and reserved words). Perhaps try renaming it to tDate
or the like in your function to be doubly-clear this isn't causing your problems.
编辑-oDate
确认包含有效日期。这可能是一个非常愚蠢的问题,但是您确认其中date
包含有效日期吗?我想知道将它命名为与关键字Date
(Javascript 关键字和保留字)相同的名称是否可能存在一些问题。也许尝试tDate
在您的函数中将其重命名为或类似名称,以双重明确这不会导致您的问题。
回答by Youssef
Use this to compare dates, it works: $("#datepickerfrom").datepicker("getDate") < $("#datepickerto").datepicker("getDate")
使用它来比较日期,它有效: $("#datepickerfrom").datepicker("getDate") < $("#datepickerto").datepicker("getDate")
回答by Javier
What is date?
什么是日期?
it's the $("#txtbox") object
这是 $("#txtbox") 对象