我正在尝试这个但是没有工作......为什么?
<html>
<body>
<script type="text/javascript">
var today=new Date(); //today is Nov 28, 2010
today.setHours(0);
today.setMinutes(0);
today.setSeconds(0);
document.write(today+" ");
var today2 = new Date("November 28, 2010");
document.write(today2 + " ");
if (today == today2) { document.write("==");
if (!(today > today2) && !(today < today2) ) {document.write("== ");}
if (today > today2) { document.write("> ");}
if (today >= today2 ){ document.write(">= ");}
if (today < today2 ) { document.write("< ");}
if (today <= today2 ){ document.write("<= ");}
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我总是得到这个:
Sun Nov 28 2010 00:00:00 GMT+0900 (JST) Sun Nov 28 2010 00:00:00 GMT+0900 (JST) > >=
Run Code Online (Sandbox Code Playgroud)
两个日期都不一样吗?因此,我应该==打印但是没有发生......;(
提前谢谢你的帮助.
use*_*716 68
它们永远不会匹配,因为您正在比较两个单独的Date对象实例.
您需要获得一些可以比较的共同价值.例如.toDateString().
today.toDateString() == today2.toDateString(); // true
Run Code Online (Sandbox Code Playgroud)
如果您只是比较两个单独的Date对象,即使它们具有完全相同的日期值,它们仍然是不同的.
例如:
today == new Date( today ); // false
Run Code Online (Sandbox Code Playgroud)
它们是相同的日期/时间值,但不是同一个对象,因此结果是false.
小智 22
function today(td) {
var d = new Date();
return td.getDate() == d.getDate() && td.getMonth() == d.getMonth() && td.getFullYear() == d.getFullYear();
}
Run Code Online (Sandbox Code Playgroud)