【问题标题】:How to check condition for date greater than or smaller than in javascript?如何在javascript中检查大于或小于日期的条件?
【发布时间】:2014-05-09 06:47:54
【问题描述】:

这里 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;
    }
}

【问题讨论】:

标签: javascript asp.net vb.net date


【解决方案1】:

假设输入日期的格式类似于 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);

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

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]);
}

【讨论】:

    【解决方案2】:

    试试这个:

    <script type="text/javascript">
        var dateObj = new Date();
        var month = dateObj.getUTCMonth();
        var day = dateObj.getUTCDate();
        var year = dateObj.getUTCFullYear();
        var dateSplitArray = "";
        var enddate = '05/05/2014'; // set your date here from txtbox
        var IsValidDate = false;
        splitString(enddate, "/");
        if (year >= dateSplitArray[2]) {
            if (month >= dateSplitArray[1]) {
                if (day >= dateSplitArray[0]) {
                    IsValidDate = true;
                }
            }
        }
        if (IsValidDate == false) {
            alert("Please ensure that the Date is greater than or equal to the Current Date.");
        }
        else {
            alert("Please proceed, no issue with date");
        }
        function splitString(stringToSplit, separator) {
            dateSplitArray = stringToSplit.split(separator);
        }
    </script>
    

    【讨论】:

    • 我的日期格式是 ex:4-may-2014。如何检查 mothng?在上面的代码中@nayeemkhan
    • 但是当月份是 eqau 并且月份是更大的条件时,我们只能添加一件事。 if (dateSplitArray[2] >= year) { if (monthNumber = month) { if (dateSplitArray[0] >= day) { IsValidDate = true; } } else if (monthNumber >month) { IsValidDate = true; }@nayeemkhan
    • 为什么使用 UTC 值?如果说 UTC-0500 的用户在 6 月 30 日 21:00 运行上述内容,则上述内容将返回 7 月 1 日的 UTC 值。此外,ECMAScript 中的月份数字是从零开始的,这个答案不允许这样做(例如,date.getMonth() 六月返回 5)。
    • @sona 在 2014 年 5 月 4 日的情况下,您可能需要维护一个月份名称数组来比较月份,谢谢。
    • @RobG 我选择 UTC 是因为日期检索很简单。您也可以转换或使用任何其他格式,没有限制:)。月份请加1。
    【解决方案3】:

    试试这个

    var date1=inputText.value;
    var date2=document.getElementById('<%=HdnDate.ClientID%>').value;
    var record1 = new Date(date1);
    var record2 = new Date(date2);
    if(record1 <= record2)
    {
          alert("Please ensure that the Date is greater than or equal to the Current Date.");
    
    }
    

    【讨论】:

    • 但是它将如何检查月份和年份?@joker
    • 它会检查整个日期而不仅仅是一天,我只是使用变量名record_day1
    • 感谢回复。让我试试。@joker
    • 对于警报消息记录 1 和记录 2,它显示的日期无效。@joker
    • 获取日期的各个部分,然后构造一个非标准字符串,然后由 Date.parse 解析,这根本没有任何意义。只需直接在 Date 构造函数中使用这些部分。例如。假设一个标准的 d/m/y 字符串:record1 = new Date(record_day1[2], --record_day1[1], record_day1[0]).
    【解决方案4】:

    试试这个代码:

    var d1= new Date(); // get the current date
    
    var enddate = inputText.value; // text box value
    
    var d2 = enddate.split('/');
    
    d2 = new Date(d2.pop(), d2.pop() - 1, d2.pop());
    
    
    if (d2 >= d1)
     {
        // do something
     }
    

    【讨论】:

    • 假设 d1 实际上是 startdate,注意 startdate 将使用当前时间而 d2 不会被设置为 00:00:00。因此,即使 d2startdate 是相同的日期,d2 &gt;= d1 几乎肯定会始终为假,除非 startdate 恰好在午夜创建(到日期开始时的毫秒 00:00:00.000)。
    猜你喜欢
    • 2016-09-25
    • 2015-03-11
    • 2014-12-26
    • 2018-03-19
    • 1970-01-01
    • 2010-10-22
    • 1970-01-01
    • 2015-06-07
    • 2016-12-11
    相关资源
    最近更新 更多