0

I have the following function in JS:

var startDate = stringToDate('12/01/2016','mm/dd/yyyy','/');
var endDate = stringToDate('01/01/2017','mm/dd/yyyy','/');


function compareDate(_date) {

  var val1 = ( startDate <= _date)
  var val2 = ( endDate >= _date )
  var val = val1 && val2

  log('--------')
  log(_date)
  log(startDate)
  log(endDate)
  log(val1)
  log(val2)
  log(val)
  log('--------')
  return val
}

LOGS

[17-02-10 13:25:14:145 EET] Fri Dec 02 2016 00:00:00 GMT+0200 (EET)

[17-02-10 13:25:14:146 EET] Thu Dec 01 00:00:00 GMT+02:00 2016

[17-02-10 13:25:14:147 EET] Sun Jan 01 00:00:00 GMT+02:00 2017

[17-02-10 13:25:14:147 EET] false

[17-02-10 13:25:14:148 EET] false

[17-02-10 13:25:14:148 EET] false

[17-02-10 13:25:14:149 EET] --------

[17-02-10 13:25:14:149 EET] --------

[17-02-10 13:25:14:150 EET] Sat Dec 03 2016 00:00:00 GMT+0200 (EET)

[17-02-10 13:25:14:150 EET] Thu Dec 01 00:00:00 GMT+02:00 2016

[17-02-10 13:25:14:151 EET] Sun Jan 01 00:00:00 GMT+02:00 2017

[17-02-10 13:25:14:151 EET] false

[17-02-10 13:25:14:152 EET] false

[17-02-10 13:25:14:152 EET] false

[17-02-10 13:25:14:152 EET] --------

[17-02-10 13:25:14:153 EET] --------

[17-02-10 13:25:14:153 EET] Sun Dec 04 2016 00:00:00 GMT+0200 (EET)

[17-02-10 13:25:14:154 EET] Thu Dec 01 00:00:00 GMT+02:00 2016

[17-02-10 13:25:14:154 EET] Sun Jan 01 00:00:00 GMT+02:00 2017

[17-02-10 13:25:14:155 EET] false

[17-02-10 13:25:14:155 EET] false

[17-02-10 13:25:14:156 EET] false

As you can see in the first log Dec 02 is greater than Dec 01 but I am getting false and so on ...

Bobj-C
  • 5,487
  • 8
  • 44
  • 75
  • 1
    Try google. http://stackoverflow.com/questions/492994/compare-two-dates-with-javascript – Sangbok Lee Feb 10 '17 at 11:35
  • What type is *_date*? I'm guessing it's a string so you're comparing a string primitive to a Date object. Please see [*How to create a minimal, complete and verifiable example*](http://stackoverflow.com/help/mcve). – RobG Feb 10 '17 at 11:46
  • Possible duplicate of [Compare two dates with JavaScript](http://stackoverflow.com/questions/492994/compare-two-dates-with-javascript) – evolutionxbox Feb 10 '17 at 11:58
  • Are you sure you are putting a date and not a string into compareDate() ie compareDate(new Date('01/01/2017')); – Andre Nel Feb 10 '17 at 12:05
  • see my date version of what you are doing below (second part of my answer)... – Andre Nel Feb 10 '17 at 12:14

1 Answers1

0

Some code is missing from your question but I added my version of it -> change string dates to ints with format yyyymmdd and compare ints for >= & <= (as string compares can be wrong):

function stringToDateInt(_date, _format, _sep)
{
  var aDate = _date  .split(_sep);
  var aForm = _format.split(_sep);
  //console.log(aDate);
  //console.log(aForm);
  var oDate = {};
  for(var i = 0, length = aDate.length; i < length; i++)
  {
    oDate[aForm[i]] = aDate[i];
  }
  //console.log(oDate);
  return (oDate.yyyy + oDate.mm + oDate.dd) * 1;
}

var startDate = stringToDateInt('12/01/2016','mm/dd/yyyy','/');
var endDate   = stringToDateInt('01/01/2017','mm/dd/yyyy','/');

function compareDate(_date)
{
  var iDate = stringToDateInt(_date,'mm/dd/yyyy','/');
  var val1 = ( startDate <= iDate);
  var val2 = ( endDate >= iDate );
  var val = val1 && val2;

  console.log('--------')
  console.log(_date, '->', iDate);
  console.log(startDate)
  console.log(endDate)
  console.log('Date Bigger than or equal startDate:', val1)
  console.log('Date Less than or equal endDate:', val2)
  console.log('Date between start and end dates:', val)
  console.log('--------')
  return val
}

compareDate('12/02/2016');

compareDate('01/01/2015');

compareDate('11/11/2017');

Or date version closer to what you are doing:

function stringToDate(_date, _format, _sep)
{
  var aDate = _date  .split(_sep);
  var aForm = _format.split(_sep);
  //console.log(aDate);
  //console.log(aForm);
  var oDate = {};
  for(var i = 0, length = aDate.length; i < length; i++)
  {
    oDate[aForm[i]] = aDate[i];
  }
  //console.log(oDate);
  //return (oDate.yyyy + oDate.mm + oDate.dd) * 1;
  return new Date(oDate.yyyy, (oDate.mm * 1 - 1), oDate.dd);
}

var startDate = stringToDate('12/01/2016','mm/dd/yyyy','/');
var endDate   = stringToDate('01/01/2017','mm/dd/yyyy','/');

function compareDate(_date)
{
  var val1 = ( startDate <= _date);
  var val2 = ( endDate >= _date );
  var val = val1 && val2;

  console.log('--------')
  console.log(_date, '->', _date);
  console.log(startDate)
  console.log(endDate)
  console.log('Date Bigger than or equal startDate:', val1)
  console.log('Date Less than or equal endDate:', val2)
  console.log('Date between start and end dates:', val)
  console.log('--------')
  return val
}

compareDate(stringToDate('12/02/2016','mm/dd/yyyy','/'));

compareDate(stringToDate('01/01/2015','mm/dd/yyyy','/'));

compareDate(stringToDate('11/11/2017','mm/dd/yyyy','/'));
Andre Nel
  • 422
  • 2
  • 9
  • You've made some bold assumptions, but the OP has not revealed how the function is called or what *stringToDate* does. Comparing `yyyymmdd` should work whether it's a string or number. BTW, in your *stringToDate*, `oDate.mm` should be `oDate.mm - 1`. ;-) – RobG Feb 10 '17 at 12:22