cancel
Showing results for 
Search instead for 
Did you mean: 

Date validation in Java script

Former Member
0 Kudos

I am writing a javascript for comparison of two dates such that the first date should be a previous date than the second date.

Since I am getting my date from a IPicker, it returns a date of the format "dd/mmmyyyy" (e.g)23/jul/2007

Now how can I compare the two dates of the above format

The following is my code which works if the date format is dd/mm/yyyy

var fromdate = document.frmdate.txtfromdate.value.split('/');

var fromdate1 = new Date(fromdate[0]'/'fromdate[1]'/'fromdate[2]);

var todate = document.frmdate.txttodate.value.split('/');

var todate1 = new Date(todate[0]'/'todate[1]'/'todate[2]);

if(fromdate1<todate1)

{

return true;

}

else

{

alert("The FromDate should be a past date than the ToDate");

return false;

}

Accepted Solutions (0)

Answers (1)

Answers (1)

Former Member
0 Kudos

Hello,

Create a function with a switch structure


function getMonth(month) {
switch (month) {
	case 'jan':
                month='01';
	break;
	case 'feb':
	month='02';
	break;
	case 'mar':
	month='03';
        break;
        //and so on.....
        }
}


Then, your main code is  : 

var fromdate = document.frmdate.txtfromdate.value.split('/');
var fromdate1 = new Date(fromdate[0]+'/'+getMonth(formdate[1])+'/'+fromdate[2]);
var todate = document.frmdate.txttodate.value.split('/');
var todate1 = new Date(todate[0]+'/'+getMonth(formdate[1])+'/'+todate[2]);

if(fromdate1<todate1)
{
return true;
} 
else 
{ 
alert("The FromDate should be a past date than the ToDate");
return false; 
}> 

Reward if helpful!

Regards,

Julien.