我有以下脚本
脚本
$('#test1').click(function () {
// Here are the two dates to compare
var date1 = '29-10-2015';
var date2 = '29-12-2015';
var Targetvalue = parseFloat("1000000");
var dealjson = '[{"dealdate":"25-12-2015","cost":200000},{"dealdate":"25-11-2015","cost":200000}]';
// First we split the values to arrays date1[0] is the year, [1] the month and [2] the day
date1 = date1.split('-');
date2 = date2.split('-');
// Now we convert the array to a Date object, which has several helpful methods
date1 = new Date(date1[2], date1[1] - 1, date1[0]);
date2 = new Date(date2[2], date2[1] - 1, date2[0]);
var deals = JSON.parse(dealjson);
var achieved = 0;
while (date1 <= date2) {
var next_day = new Date(date1);
next_day.setDate(date1.getDate() + 1);
achieved = 0;
deals.forEach(function (deal) {
var dealDate = deal.dealdate;
dealDate = dealDate.split('-');
dealDate = new Date(dealDate[2], dealDate[1] - 1, dealDate[0]);
if (dealDate === date1) console.log("matched" + date);
});
date1 = next_day;
}
});
我正在尝试登录控制台,如果循环中的日期都与Json数组dealjson
中的日期匹配,尽管我有两个日期介于date1和date 2之间,但仍然没有发生匹配
if (dealDate === date1) console.log("matched" + date);
以下是FIDDLE
任何人都可以帮我解决错误
答案 0 :(得分:0)
要在JavaScript中检查两个相等的日期,您必须获得自1970年1月1日00:00:00 UTC以来的&#34; 毫秒数&#34;并比较这些值。
要获取此号码,请使用日期getTime()
方法:
if (dealDate.getTime() === date1.getTime()) console.log("matched" + date);
查看更新的JSFiddle demo。
答案 1 :(得分:0)
当您使用==
或===
来比较Date
个实例时,相同的Date
实例才会出现这种情况,而不是等效的 Date
个实例。
要查看它们是否完全相同,请使用+
或getTime()
:
if (+dealDate === +date1)
// or
if (dealDate.getTime() === date1.getTime())
这不是您的其他日期关系<=
的问题,因为<=
强制其对基元的参数。如果双方都是对象,则==
和===
不会这样做。
答案 2 :(得分:0)
您需要将日期放在时间戳中。
var dc1=new Date(dealDate).getTime();
var dc2=new Date(date1).getTime();
if(dc1===dc2) console.log("matched"+date1);
$('#test1').click(function () {
// Here are the two dates to compare
var date1 = '29-10-2015';
var date2 = '29-12-2015';
var Targetvalue = parseFloat("1000000");
var dealjson = '[{"dealdate":"25-12-2015","cost":200000},{"dealdate":"25-11-2015","cost":200000}]';
// First we split the values to arrays date1[0] is the year, [1] the month and [2] the day
date1 = date1.split('-');
date2 = date2.split('-');
// Now we convert the array to a Date object, which has several helpful methods
date1 = new Date(date1[2], date1[1]-1, date1[0]);
date2 = new Date(date2[2], date2[1]-1, date2[0]);
var deals = JSON.parse(dealjson);
var achieved = 0;
while (date1 <= date2) {
var next_day = new Date(date1);
next_day.setDate(date1.getDate() + 1);
achieved = 0;
deals.forEach(function (deal) {
var dealDate = deal.dealdate;
dealDate = dealDate.split('-');
dealDate = new Date(dealDate[2], dealDate[1]-1, dealDate[0]);
var dc1=new Date(dealDate).getTime();
var dc2=new Date(date1).getTime();
if(dc1==dc2){
console.log("matched"+date1);
$(".varianceData").text("matched"+date1);
}
});
date1 = next_day;
}
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<button id="test1">See the Tab Day </button>
<div class="varianceData"></div>
&#13;