0

Following is my jquery code, I want to compare the date pick by user with my gstdate.

$('#FromDate').on('blur', function() {
  var fromdate = $('#FromDate').val();
  var gstdate = "1-7-2017";
  var gd = new Date('d-m-Y', gstdate);

  if (fromdate <= gst) {
    //do something..
  } else {
    //do something..
  }
});
Milan Chheda
  • 8,159
  • 3
  • 20
  • 35
  • Possible dup of https://stackoverflow.com/questions/5619202/converting-string-to-date-in-js – Dat Nguyen Jul 24 '17 at 06:05
  • 2
    Possible duplicate of [Converting string to date in js](https://stackoverflow.com/questions/5619202/converting-string-to-date-in-js) – Dat Nguyen Jul 24 '17 at 06:05

3 Answers3

0

You need to convert to the fromDate into date variable as you did with gstDate

$('#FromDate').on('blur', function() {
  var fromdate = new Date("d-m-Y", $('#FromDate').val());
  var gstdate = "1-7-2017";
  var gd = new Date('d-m-Y', gstdate);

  if (fromdate <= gst) {
    //do something..
  } else {
    //do something..
  }
});
Milan Chheda
  • 8,159
  • 3
  • 20
  • 35
Hannan Ayub
  • 378
  • 2
  • 15
  • var gstdate = "1-7-2017"; var gd = new Date('d-m-Y',gstdate); This code does not converting my gstdate in a date format. In console I have got an error : invalid date – Dwarkesh Purohit Jul 24 '17 at 06:23
  • Thanks, it worked ! Still in the date comparison having trouble using this way [ if(fromdate <= gst) ]. – Dwarkesh Purohit Jul 25 '17 at 04:22
0

You can try momentJs

I hope I've helped you!

xKobalt
  • 1,498
  • 2
  • 13
  • 19
Tran Audi
  • 587
  • 1
  • 6
  • 22
0
$('#FromDate').on('blur', function() {
  var gstdate = new Date("1-7-2017"); //date object required
  var fromdate = new Date($(this).val());
    console.log(gstdate);
  if (fromdate.getTime() <= gstdate.getTime()) {// getTime() returns the number of milliseconds
    console.log('hello');
  } else {
  console.log('bye');
  }
});
KARAN LAGALWAR
  • 227
  • 1
  • 8
  • having an error log : fromdate.getTime() is not a function. – Dwarkesh Purohit Jul 25 '17 at 04:23
  • Please refer this [https://www.w3schools.com/js/js_date_methods.asp] in this **getTime()** is a function For that **date object** is required and your gst date is not in formatted it should be **mm-dd-yyyy** – KARAN LAGALWAR Jul 25 '17 at 05:57