Click here to Skip to main content
15,867,993 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
Hi,

I would like to filter fromDate and toDate.
when i am doing search with fromDate as Jan -30-2017 and toDate as Feb-01-2017 it is showing a message "ToDate" Cannot be less than "FromDate".

Note:
Senario 1: For Every year search for particularly Jan-30 To Feb-01.
Senario 2: For Every year search for particularly Jan-31 To Feb-01.

In other cases working fine.

JavaScript
ValidateDate(fromDate,toDate){

var pDay=parseInt(toDtae.substring(0,2),10);
var pMon=parseInt(toDtae.substring(0,2),10);
var pYear=parseInt(toDtae.substring(0,2),10);
var pDate=new Date(pYear,pMon,pDay);

var oDay=parseInt(toDtae.substring(0,2),10);
var oMon=parseInt(toDtae.substring(0,2),10);
var oYear=parseInt(toDtae.substring(0,2),10);
var oDate=new Date(oYear,oMon,oDay);

if(pDate<oDate){
confirm('<s:Text name="labe.dateerrorMessage"/>')
documentation("okButton").disabled=true;
}
}

whenever i am searching fromDate Jan 30/31 to Feb 01 for Every year it is showing
"ToDate" Cannot be less than "FromDate".

Example: fromDate:jan302017
toDate:Feb012017
selection is ahppening through Calander.

What I have tried:

JavaScript
ValidateDate(fromDate,toDate){

var pDay=parseInt(toDtae.substring(0,2),10);
var pMon=parseInt(toDtae.substring(0,2),10);
var pYear=parseInt(toDtae.substring(0,2),10);
var pDate=new Date(pYear,pMon,pDay);

var oDay=parseInt(toDtae.substring(0,2),10);
var oMon=parseInt(toDtae.substring(0,2),10);
var oYear=parseInt(toDtae.substring(0,2),10);
var oDate=new Date(oYear,oMon,oDay);

if(pDate<oDate){
confirm('<s:Text name="labe.dateerrorMessage"/>')
documentation("okButton").disabled=true;
}
}
Posted
Updated 2-Feb-17 2:05am
v2
Comments
Nathan Minier 2-Feb-17 7:43am    
So...you did notice that you're parsing the same thing (toDtae) which isn't event one of the variables that you passed to the function, and that your operator choice ignores the equals case, right?
CHill60 2-Feb-17 7:48am    
That'll be the solution :-)
Nathan Minier 2-Feb-17 7:51am    
In this case I'm going with a maybe.You'll notice each parseint function is being applied to the same substring as well. There's basically nothing right.
CHill60 2-Feb-17 7:55am    
That's a very polite description of what is wrong with the code.
Jochen Arndt 2-Feb-17 8:00am    
Besides the already mentioned errors in parsing always the same characters for all date portions:

Don't use strings for date operations if possible.

Use native date, time, and datetime types instead.

Create strings only for displaying them. If you have to parse strings containing dates ensure that you know the used format.

Doing so will make working with dates much easier and avoids conversion errors.

1 solution

Okay, so you want a simple JavaScript date comparison.

JavaScript
function ValiDate(startText,endText) //get it?
{
   var startDate = new Date(startText), 
       endDate = new Date(endText);

   if(startDate.getTime() > endDate.getTime()){
      alert('ZOMG errorz');
      document.getElementById('okButton').disabled = true;
   }
}
 
Share this answer
 
v3
Comments
Member 12976910 2-Feb-17 9:33am    
Hi,

Now it is in reverse case. Not working.
Please read the question clearly
Richard MacCutchan 2-Feb-17 10:00am    
Might be an idea if you read the question carefully. You would then see why it will never work.
Nathan Minier 2-Feb-17 10:08am    
Tell ya what, I just edited the sample to make it less sensible from a validation standpoint. Enjoy.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900