首页 > 解决方案 > 日期比较在 jQuery 侦听器中不起作用

问题描述

警报显示每个更改事件,但if条件不起作用。如何解决?

这是我的代码。

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

标签: javascriptjquerydate-comparison

解决方案


尝试在比较之前将您的日期变量转换为对象。

例如:

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

if((new Date(date) > todaydates))
{
   alert('date is greater than today date');
}

推荐阅读