Compare two mmddyyyy dates in JavaScript

I need to compare two dates in JavaScript that are entered as MMDDYYYY and throw an error if one is before the other.

In the .js file, any chars are stripped out from the two fields.

var cmDateOfInjury = stripCharsNotInBag (form.cmDateOfInjury.value, "1234567890");
             form.cmDateOfInjury.value = cmDateOfInjury;
var cmDateNotify = stripCharsNotInBag (form.cmDateNotify.value, "1234567890");
             form.cmDateNotify.value = cmDateNotify;

The cmDateNotify cannot be before the cmDateOfInjury. How do I convert the dates and compare?
LVL 1
roosterupAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GaryCommented:
Are the dates exactly like MMDDYYYY i.e. no seperators?
0
Lee SavidgeCommented:
I'd jiggle with the strings first.

Make them in the form yyyymmdd and then do an integer compare.

eg

<script>

var doi = "12252013";
var don = "12302013";

doi = doi.substring(4) + doi.substring(0, 4);
don = don.substring(4) + don.substring(0, 4);

if(parseInt(don) < parseInt(doi))
  document.write("invalid, notify date cannot be earlier");
else
  document.write("this is ok");


</script>

Open in new window

0
Lee SavidgeCommented:
Try it for yourself. Go here first and then replace the code with the below

http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_substring

<!DOCTYPE html>
<html>
<body>

<script>

var doi = "12252013";
var don = "12302013";

doi = doi.substring(4) + doi.substring(0, 4);
don = don.substring(4) + don.substring(0, 4);

if(parseInt(don) < parseInt(doi))
  document.write("invalid");
else
  document.write("valid");


</script>

</body>
</html>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

roosterupAuthor Commented:
Yes, the dates are MMDDYYYY  with no separators.
0
GaryCommented:
lsavidge has given you a nice easy solution for your case then.
0
roosterupAuthor Commented:
awesome.
0
Lee SavidgeCommented:
Thanks :)

Whenever playing dates and you're doing a date comparison, I always and I mean I ALWAYS, convert to an integer and compare. It's a lot easier to do in code. Put some comments in the code because sometimes it may be a bit weird to someone that doesn't use this method.

It makes sorting lists of dates a doddle as well.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.