javascript - 如何在javascript中检查日期大于或小于的条件?

标签 javascript asp.net vb.net date

此处的 inputtext 给出在文本框中输入的值,隐藏字段值返回当前值。

到目前为止我的代码:

if (inputText.value.length != 0) {
    if (inputText.value < document.getElementById('<%=HdnDate.ClientID%>').value) {
        alert("Please ensure that the Date is greater than or equal to the Current Date.");
        inputText.value = "";
        return false;
    }
}

最佳答案

假设输入日期的格式为 d/m/y,那么您可以使用以下方法将其转换为日期对象:

function parseDate(s) {
  var b = s.split(/\D+/g);
  return new Date(b[2], --b[1], b[0]);
}

它创建指定日期 00:00:00 的日期对象。

要与当前日期进行比较,请创建一个新的 Date 对象并将时间设置为 00:00:00.0:

var today = new Date();
today.setHours(0,0,0,0);

然后将字符串转换为日期并比较两者:

var otherDay = parseDate('21/4/2013');

console.log(otherDay + ' less than ' + today + '?' + (otherDay < today)); // ... true

编辑

您的日期格式似乎是 2014 年 5 月 4 日。在这种情况下:

function parseDate(s) {
  var months = {jan:0,feb:1,mar:2,apr:3,may:4,jun:5,
                jul:6,aug:7,sep:8,oct:9,nov:10,dec:12};
  var b = s.split(/-/g);
  return new Date(b[2], months[b[1].substr(0,3).toLowerCase()], b[0]);
}

关于javascript - 如何在javascript中检查日期大于或小于的条件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23558245/

相关文章:

javascript - ReportViewer Web 窗体导致页面挂起

vb.net - 从查询字符串中获取数据

php - 为此使用什么算法?

asp.net - 无需物理路径,将文件上传控件中的 CSV 直接插入到内存流中

javascript - react : How does React make sure that useEffect is called after the browser has had a chance to paint?

javascript - Angular CoffeeScript 语法错误

javascript - 使 intro.js 工具提示向右强制并使页面滚动

c# - C# 中的自然排序顺序 - 实现

c# - 撤销 OAuthBearerAuthentication 的访问 token

php - HTML5 数据库连接丢失