0

I have following script

Script

$('#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;
    }

});

I am trying to log in the console if both the date in the loop matches the date from Json array dealjson although I have two dates which falls in between the date1 and date 2 but still the match is not happening

if (dealDate === date1) console.log("matched" + date);

Here is the FIDDLE

Can any one help me out where is the mistake

Vikram Anand Bhushan
  • 4,836
  • 15
  • 68
  • 130

3 Answers3

0

To check two dates for equality in JavaScript you have to get their "number of milliseconds since 1 January 1970 00:00:00 UTC" and compare these values.

To get this number, use the Date's getTime() method:

if (dealDate.getTime() === date1.getTime()) console.log("matched" + date);

See updated JSFiddle demo.

Andrea
  • 3,370
  • 1
  • 17
  • 25
0

When you use == or === to compare Date instances, it will only be true for the same Date instance, not for equivalent Date instances.

To see if they have exactly the same time, use + or getTime():

if (+dealDate === +date1)
// or
if (dealDate.getTime() === date1.getTime())

This isn't a problem with your other date relation, <=, because <= coerces its arguments to primitives. == and === don't do that if both sides are objects.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • @T.JCrowder can you instruct me How I can calculate total number of months between two given dates and then match the date from the json and log into console if the date match for that particular month – Vikram Anand Bhushan Oct 15 '15 at 06:07
0

You need to put date in timestamp.

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;
    }

});
<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>
Ivijan Stefan Stipić
  • 6,249
  • 6
  • 45
  • 78