0
<script>  
  dfrom = datefrom.split("/");
    dto = dateto.split("/");

     //Checking Year Part;
     if(parseInt(dfrom[2]) > parseInt(dto[2])){
      alert("DateFrom Cannot greater than DateTo");
      return false;
     }
     if((parseInt(dfrom[1]) > parseInt(dto[1])) && parseInt(dfrom[2]) == parseInt(dto[2])){
      alert("DateFrom Cannot greater than DateTo");
      return false;
     }
     if(parseInt(dfrom[0]) > parseInt(dto[0]) && (parseInt(dfrom[1]) == parseInt(dto[1])) &&    parseInt(dfrom[2]) == parseInt(dto[2])){
      alert("DateFrom Cannot greater than DateTo");
      return false;
     }
</script>

This is my script code to compare dates and is working fine but when I check 07/04/2013 and 08/04/2013, it shows "DateFrom Cannot greater than DateTo" and only these dates are showing wrong result. Is any error in my script or something else?

Any help would be highly appreciable.

4
  • 2
    Use a javascript Date library. It's much easier. Commented Mar 25, 2013 at 4:06
  • use parseInt(something,10), or the '08' and '09' strings will be octal numbers. Commented Mar 25, 2013 at 4:20
  • The code as written is (semi-)nonsense. You have return statements but no enclosing function. Even adding the right plumbing (and never use parseInt with out a radix), the code seems to work with the dates mentioned, but then again who knows if I'm altering the code in a way that you didn't have... Commented Mar 25, 2013 at 4:28
  • 1
    Don't bother doing it yourself use momentjs Commented Mar 25, 2013 at 4:30

3 Answers 3

1

try this

dfrom = datefrom.split("/");
dto = dateto.split("/");


var x=new Date();
x.setFullYear(dfrom [2],dfrom [1]-1,dfrom [0]);

 var y=new Date();
y.setFullYear(dto [2],dto [1]-1,dto [0]);


if (x>y)
  {
  alert("X is big ");
  }
else
  {
  alert("Y is big");
  }

see here

Sign up to request clarification or add additional context in comments.

1 Comment

+1 for using Date objects, but there's no need to create a date object then modify the values, you can pass the values to the constructor and save a step and function call.
1

When interpreting the parseInt function's arguments, older browsers will use the octal radix (base-8 numbering system) as default when the string begins with "0" (e.g., '07', '08'). As of ECMAScript 5, the default is the decimal radix (10) (i.e., this is tricky, but at least now it is depreciated).

In other words, there is a chance that if you pass strings ("01") or numbers (01) that begin with 0 to parseInt without specifying the second parameter (radix, which means what numbering system), they will be interpreted as having radix 8. This means 07 === 7 and 08 probably has undefined behavior (0, "", undefined, who knows?).

To be safe, always set your radix in the second parameter to parseInt when dealing with dates (I know I do), for example parseInt(x, 10) for regular base 10.

By the way, leading numbers with 0 indicates the octal radix other languages, so it is a good to get rid of them when converting strings to numbers.

Good luck!

1 Comment

While you are correct about ES5 changing how leading zeros are treated, not all browsers have implemented it yet (if any). The octal treatment isn't deprecated, it's removed.
0

The easiest way to compare date strings is to turn them into date objects and compare those, so if your date strings are in the format d/m/y. you can do:

// s in format d/m/y
// e.g. 15/3/2013 or 15/03/2013
function toDate(s) {
  var s = s.split('/');
  return new Date(s[2], --s[1], s[0]);
}

var d0 = '3/3/2013';
var d1 = '15/3/2013';

// Compare dates
alert( toDate(d0) < toDate(d1) );  // true

alert( toDate(d1) < toDate(d0) );  // false

When used in a comparison or arithmetic operation, Date objects are coerced to a number that is their time value.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.