0

The alert is showing every change event, but the if condition is not working. How to fix it?

Here is my code.

$("input").change(function() {
  var date = document.getElementById('<%= txtRegisteredDate.ClientID%>').value;
  var todaydates = new Date().format('dd/MM/yyyy');

  if (date > todaydates) {
    alert('date is greater than today date');
  }
})
Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
  • 3
    There is no `format` function in Date – Jack jdeoel Jul 03 '18 at 10:16
  • 1
    Even if `.format` worked. Why are you comparing _strings_ in a `dd/MM/yyyy` format? If you wanted to compare strings, you’d use `yyyy-mm-dd`. But you can just compare `Date` objects directly: `new Date() < someOtherDate`. – Sebastian Simon Jul 03 '18 at 10:19
  • 2
    Possible duplicate of [Compare two dates with JavaScript](https://stackoverflow.com/questions/492994/compare-two-dates-with-javascript) – Sebastian Simon Jul 03 '18 at 10:19
  • You might want to look at this question https://stackoverflow.com/questions/23641525/javascript-date-object-from-input-type-date – tenor528 Jul 03 '18 at 10:21

1 Answers1

0

Try converting your date variables to an object before the comparison.

e.g:

var date = document.getElementById('<%= txtRegisteredDate.ClientID%>').value;
var todaydates = new Date();

if((new Date(date) > todaydates))
{
   alert('date is greater than today date');
}
DNKROZ
  • 2,634
  • 4
  • 25
  • 43