简体   繁体   English

比较格式为dd-MMM-yyyy的日期的验证

[英]Compare Validation for dates of format dd-MMM-yyyy

I'm having two text boxes. 我有两个文本框。 One contains the start date and the other contains the end date. 一个包含开始日期,另一个包含结束日期。 Both of the text boxes are in dd-MMM-yyyy date format. 两个文本框均为dd-MMM-yyyy日期格式。 My requirement is to compare these two dates and if the To date is less than the From date i have to throw an exception(I must not proceed further if To date < than From Date). 我的要求是比较这两个日期,如果“到”日期小于“从”日期,则必须抛出一个异常(如果“到”日期小于“从”,则我不能继续进行)。

I have used compare validator, but it doesn't work. 我已经使用了比较验证器,但是它不起作用。 so I have tried to do the validation using java script.But it is not working for some days. 因此,我尝试使用Java脚本进行验证。但是,该功能已无法使用几天了。

I have written the script as: 我将脚本编写为:

function ValidateDate() {
        var fromdate = document.getElementById("txtStartDate");
        var todate = document.getElementById("txtEndDate");

        var startDate = fromdate.value;
        var endDate = todate.value;


        if (endDate < startDate) {
            alert("Please ensure that the End Date is greater than or equal to the Start Date.");
            return false;
        }
    }

Please any one of you provide a solution for me. 请你们中的任何一个为我提供解决方案。 Thanks in advance. 提前致谢。

Use of Jquery here can be lot easier. 在这里使用Jquery会容易得多。

var fromdate = document.getElementById("txtStartDate");
    var todate = document.getElementById("txtEndDate");

if ($.datepicker.parseDate('dd/mm/yy', fromdate) > $.datepicker.parseDate('dd/mm/yy', todate)) {

       alert(fromdate+ 'is greater than ' + todate);

}

Check THIS doccument. 检查文档。

To compare two dates, c# provides a method to compare them. 为了比较两个日期,c#提供了一种比较它们的方法。 DateTime.Compare . DateTime.Compare It returns an int value to check if the first is earlier, the same or later than the second. 它返回一个int值,以检查第一个是否早于,相同或晚于第二个。

DateTime date1 = new DateTime(2013, 8, 1, 0, 0, 0);
DateTime date2 = new DateTime(2013, 8, 1, 12, 0, 0);
int result = DateTime.Compare(date1, date2);

if (result < 0)
{
    // date1 is earlier than date2;
}
else if (result == 0)
{
    // date1 is the same time as date2
}
else
{
    // date1 is later than date2
}

Hope it helps you. 希望对您有帮助。

I assume you need to convert the textboxes' values to Date before you can compare them. 我认为您需要先将文本框的值转换为Date然后才能进行比较。 Otherwise, you would be just comparing their lexicographical order. 否则,您将只是比较它们的字典顺序。

var startDate = Date.parse(fromdate.value);
var endDate = Date.parse(todate.value);

Try something like. 尝试类似的东西。

function CompareDates(str1,str2) 

{ 

    if(str1 == "")

        return false;

   var dt1   = parseInt(str1.substring(0,2),10); 

   var mon1  = parseInt(str1.substring(3,5),10);

   var yr1   = parseInt(str1.substring(6,10),10); 

   var dt2   = parseInt(str2.substring(0,2),10); 

   var mon2  = parseInt(str2.substring(3,5),10); 

   var yr2   = parseInt(str2.substring(6,10),10); 

   mon1 = mon1 -1 ;

   mon2 = mon2 -1 ;

   var date1 = new Date(yr1, mon1, dt1); 

   var date2 = new Date(yr2, mon2, dt2); 



   if(date2 >= date1)

   {

        return false; 

   } 

   else 

   { 

        return true;

   } 

} 

Finally, I got the solution Friends... 最后,我得到了解决方案的朋友...

What I have done is I just parsed the date to milliseconds since January 1, 1970, 00:00:00 UTC. 我所做的是,我只是将日期解析为UTC 1970年1月1日00:00:00以来的毫秒数。 I have parsed for both the dates and compared those two values.. 我已经解析了两个日期并比较了这两个值。

The solution for it is: 解决方案是:

function ValidateDate() {
        var fromdate = document.getElementById("txtFromDate");
        var todate = document.getElementById("txtToDate");
        var endDate = todate.value;
        var startDate = fromdate.value;

        /* Convert the date 01-Jan-1991 to Jan 1, 1991 */

        var splitdate = startDate.split("-");
        var dt1 = splitdate[1] + " " + splitdate[0] + ", " + splitdate[2];
        var splitdate = endDate.split("-");
        var dt2 = splitdate[1] + " " + splitdate[0] + ", " + splitdate[2];

        var newStartDate = Date.parse(dt1); //Parse the date to  milliseconds since January 1, 1970, 00:00:00 UTC
        var newEndDate = Date.parse(dt2);

        if (newStartDate > newEndDate ) {
            alert("Please ensure that the End Date is greater than or equal to the Start Date.");
            return false;
        }
    }

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

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