0

I am facing one problem. I am unable to differentiate the date difference using the javascript. I am explaining my code below.

var startdate_val = document.getElementById("stdate").value;
var enddate_val = document.getElementById("enddate").value;
var one_day=1000*60*60*24; 
var x=startdate_val.split("-");     
var y=enddate_val.split("-");
var date1=new Date(x[2],(x[1]-1),x[0]);
var date2=new Date(y[2],(y[1]-1),y[0])
var month1=x[1]-1;
var month2=y[1]-1;
var date_diff = Math.ceil((date2.getTime()-date1.getTime())/(one_day));
console.log('date',date_diff <= 0);

Here I need enddate always should be greater than the start date. Here I am attaching my datetime code.

<div class="row">
<div class="col-lg-6 col-md-6 col-sm-6">
<div class="pad-bot-10">
<label for="raised">Start Date</label>
<input id="stdate" type="text" class="form-control datetime" value="" placeholder="17-06-2017"/>
    </div>
</div>
<div class="col-lg-6 col-md-6 col-sm-6">
<div class="pad-bot-10">
<label for="raised">End Date</label>
<input id="enddate" type="text" class="form-control datetime" value="" placeholder="17-06-2017"/>
      </div>
  </div>
</div>  

Here I am getting the date field value like this i.e-03-01-2018 02:46. I need always the end date time should be greater than the start date time but in my case in console message always I am getting the result false.

3 Answers3

0
Try this:
<div class="row">
<div class="col-lg-6 col-md-6 col-sm-6">
<div class="pad-bot-10">
<label for="raised">Start Date</label>
<input id="stdate" type="text" class="form-control datetime" value="17-06-2017" placeholder="17-06-2017"/>
    </div>
</div>
<div class="col-lg-6 col-md-6 col-sm-6">
<div class="pad-bot-10">
<label for="raised">End Date</label>
<input id="enddate" type="text" class="form-control datetime" value="17-07-2017" placeholder="17-06-2017"/>
      </div>
  </div>
</div>  

var startdate_val = document.getElementById("stdate").value;
var enddate_val = document.getElementById("enddate").value;
var one_day=1000*60*60*24; 
var x=startdate_val.split("-");     
var y=enddate_val.split("-");
var date1=new Date(x[2],(x[1]-1),x[0]);
var date2=new Date(y[2],(y[1]-1),y[0])
var month1=x[1]-1;
var month2=y[1]-1;
var date_diff = Math.ceil((date2.getTime()-date1.getTime())/(one_day));
console.log(date_diff >= 0);

You just need to change the line: console.log('date',date_diff <= 0);

65th bit
  • 55
  • 1
  • 11
-1

Simply do like this :

var startdate_val = document.getElementById("stdate").value;
var enddate_val = document.getElementById("enddate").value;
var stdate = new Date(startdate_val).toISOString();
var enddate = new Date(enddate_val).toISOString();
//Then
Console.log(enddate >= stdate);

Sample Code that I've tried:

var startdate_val = '01-03-2018 02:45';
var enddate_val = '01-03-2018 02:45';
var stdate = new Date(startdate_val);
var enddate = new Date(enddate_val);
//Then
alert(enddate >= stdate);

Returns True

Fiddle

Converting the string to Date Object will be better than the manual labor.

Arijit Mukherjee
  • 3,817
  • 2
  • 31
  • 51
  • Using the built-in parser is not recommended. The OP format appears to be d/m/y, however most built-in parers will parse '03-01-2018 02:45' as 1 March, not 3 January. – RobG Jan 03 '18 at 00:42
  • @RobG OP has deleted the comments I guess i told OP to parse with format as well as UTC – Arijit Mukherjee Jan 03 '18 at 05:07
  • 1
    If you think those comments are important, you should include them in your answer. It should stand alone and not depend on comments elsewhere. In Safari, `new Date('01-03-2018 02:45')` returns an invalid date (which is as consistent with ECMA-262 as any other result) so the comparison becomes `NaN >= NaN`. – RobG Jan 03 '18 at 08:53
-1

in your HTML code, there is no value so you can't compare it to page load time, because it is blank. somehow if you are using any click functionality then please update your code below.

    var startdate_val = document.getElementById("stdate").value;
        var enddate_val = document.getElementById("enddate").value;
        var one_day = 1000 * 60 * 60 * 24;
        var x = startdate_val.split("-");
        var y = enddate_val.split("-");
        var date1 = new Date(x[2], (x[1] - 1), x[0]);
        var date2 = new Date(y[2], (y[1] - 1), y[0])
        var month1 = x[1] - 1;
        var month2 = y[1] - 1;
        var date_diff = Math.ceil((date2.getTime() - date1.getTime()) / (one_day));
        if (date_diff <= 0)
            console.log(false);
        else
            console.log(true);

AND HTML FOR PAGE LOAD

  <div class="row">
        <div class="col-lg-6 col-md-6 col-sm-6">
            <div class="pad-bot-10">
                <label for="raised">Start Date</label>
                <input id="stdate" type="text" class="form-control datetime" value="17-06-2017" placeholder="17-06-2017" />
            </div>
        </div>
        <div class="col-lg-6 col-md-6 col-sm-6">
            <div class="pad-bot-10">
                <label for="raised">End Date</label>
                <input id="enddate" type="text" class="form-control datetime" value="18-06-2017" placeholder="17-06-2017" />
            </div>
        </div>
    </div>
Negi Rox
  • 3,828
  • 1
  • 11
  • 18