JavaScript - How to compare two dates by using JavaScript

Asked By Lalitha Chevuru on 15-Feb-05 06:12 AM
hi,
   This is Lalitha, i am new to JavaScript and this is my first post in this forum. I want to compare two dates like from date and TO date which is in the format mm/dd/yyyy.
if To date is less than from date then i wanna display the message.
Plz help me out.

Thanks in advance.

Lalitha.C

You would use the Date object - Asked By Robbe Morris on 15-Feb-05 07:15 AM

And simply to a <  or > comparison in an if statement.

 var Date1 = new Date("12/31/2004");
 var Date2 = new Date("1/5/2005");

 if (Date2 > Date1)
 {
     alert('found');
 }
Asked By Thiru Dotnte on 29-Mar-05 01:16 AM
hi Lalitha,

Check  out this

var todate = new Date(todatestring);
var fromdate = new Date(fromdatestring);

if (todate < fromdate)
{ 
alert('Error');
}

Rgds,
Thiru

the following code may be useful to you. - Sreekanth Goud replied to Lalitha Chevuru on 17-Jan-08 04:31 AM

Hi,

This is SreekanthGoud. The following code may usful to your problem.

Regards

Sreekanth.


function checkDates(){

var myDate = date + noOfDays( month, year ) + (year*365);
myDate = parseInt( myDate );
var currDate1 = new Date();
currDate = currDate1.getDate() + noOfDays(currDate1.getMonth(), currDate1.getYear() ) +( currDate1.getYear()*365);
currDate = parseInt( currDate );
if(myDate > currDate){
   alert( "myDate > currDate." );
} else {

   alert( "myDate < currDate." );

}

}/*checkDates()*/

function noOfDays( month, year ){
 var days = 0;
 month = month + 1;

 for( var i=1; i<month; i++ ){
        switch( i ){
            case 1:case 3:case 5:case 7:case 8:case 10:
                days = days + 31;
                break;
            case 2:
                if( checkleapyear(year) ){
                    days = days + 29;
                } else {
                    days = days + 28;
                }
                break;
            case 4:case 6:case 9:case 11:
                days = days + 30;
                break;
            default:
                break;
        }/*swith*/
 }/*for-loop*/
 return days;
}/*noOfDays()*/


function checkleapyear(datea){
 datea = parseInt(datea);

 if(datea%4 == 0){
  if(datea%100 != 0){
   return true;
  }else{
   if(datea%400 == 0)
    return true;
   else
    return false;
  }
 }
return false;
}/*checkleapyear()*/

Js code for dates - Samuel Dondapati replied to Lalitha Chevuru on 28-Jul-08 07:25 AM

function ValidateDate()

{

if(document.forms['recentUpdates'].preDefinedDate[6].checked)

{

var fromDate = document.forms['recentUpdates'].elements['fromDate'].value;

var toDate = document.forms['recentUpdates'].elements['toDate'].value;

var alertReason1 = 'To Date must be greater than or equal to From Date.'

var endDate = new Date(toDate);

var startDate= new Date(fromDate);

if(fromDate == '' && toDate == '')

{

alert("Please enter From and To Dates");

return false;

}

else if(fromDate == '')

{

alert("Please enter From Date ");

return false;

}

else if(toDate == '')

{

alert("Please enter To Date ");

return false;

}

else if (fromDate !='' && toDate != '' && startDate > endDate)

{

alert(alertReason1);

return false;

}

}

}

 

In the above in place of recentUpdates, need to put ur fom name that is present in the name attribute of the form tag

Compare two dates - murugesan g replied to Lalitha Chevuru on 08-May-09 05:36 AM
<script language="javascript" type="text/javascript">
function DateValidate(CtrlSDate, CtrlEDate)
    {
            var SDate = document.getElementById(CtrlSDate).value;        
            var EDate =  document.getElementById(CtrlEDate).value;
             
            var firstIndex = SDate.indexOf("-");
            var secondIndex = SDate.lastIndexOf("-");
            
            var d1 = SDate.substring(0,firstIndex);
            var m1 = SDate.substring(firstIndex+1,secondIndex);
            var y1 = SDate.substring(secondIndex+1,SDate.length);
            var SDateFull=m1 + "/" + d1 + "/" +y1;
            
            
            var d2 = EDate.substring(0,firstIndex);
            var m2 = EDate.substring(firstIndex+1,secondIndex);
            var y2 = EDate.substring(secondIndex+1,EDate.length);
            var EDateFull=m2 + "/" + d2 + "/" +y2;
            
         
             var startDate = new Date(SDateFull);
             var endDate = new Date(EDateFull);
            
            if(SDate != '' && EDate != '' && startDate > endDate)
            {
                alert('TO date must be greater than FROM date.');
                return false;
            }
        return true;  
    }
 </script>

C# Code

button1.Attributes.Add("onClick", "javascript:return DateValidate('" + txtFromDate.ClientID + "','" + txtToDate.ClientID + "')");


Kushal replied to Lalitha Chevuru on 04-Mar-11 03:36 AM

Thanks murugesan g!

Very useful example.