Date and Time validation using java script

Introduction:

  How to validate from date and to date then show msg using java script.

Solution:

 step 1: first create two text boxes are as From Date and To Date with date picker in asp.net with c#.

 step 2: The following java script code should be use ful for date validation.

codings:

var months = { 'Jan': 1, 'Feb': 2, 'Mar': 3, 'Apr': 4, 'May': 5, 'Jun': 6, 'Jul': 7, 'Aug': 8, 'Sep': 9, 'Oct': 10, 'Nov': 11, 'Dec': 12 };
      
        var d1 = document.getElementById(txt_FromDate).value.split('-');
        var d2 = document.getElementById(txt_toDate).value.split('-');
    
        var date1 = new Date(parseInt(year[0]), parseInt(months[d1[1]]) - 1, parseInt(d1[0]));
        var date2 = new Date(parseInt(d2[2]), parseInt(d2[1])-1, parseInt(d2[0]));

        if (date2 < date1) {
            alert("Date and Time is Future Date.");
            document.getElementById(txt_FromDate).focus();
            return false;
        }

Comments