简体   繁体   English

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

[英]How to check condition for date greater than or smaller than in javascript?

here inputtext gives the value entered in the textbox and hidden field value returns current value. 在这里,inputtext给出了在文本框中输入的值,而隐藏字段的值则返回当前值。

My code till now: 到目前为止,我的代码:

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

Assuming that the input date is in a format like d/m/y, then you can convert that to a date object using: 假设输入日期格式为d / m / y,则可以使用以下命令将其转换为日期对象:

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

which creates a date object for 00:00:00 on the specified date. 它将在指定日期为00:00:00创建一个日期对象。

To compare to the current date, create a new Date object and set the time to 00:00:00.0: 要与当前日期进行比较,请创建一个新的Date对象并将时间设置为00:00:00.0:

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

Then convert the string to a Date and compare the two: 然后将字符串转换为日期并比较两者:

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

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

Edit 编辑

It seems your date format is 4-may-2014. 您的日期格式似乎是2014年5月4日。 In that case: 在这种情况下:

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

Try this code : 试试这个代码:

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
 }

Try This 尝试这个

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.");

}

Try This: 尝试这个:

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM