Convert text variables containing mm-dd-yyyy to a date format so I can compare

Uncle, I cry uncle....  I've browsed a bunch of code and can't seem to hone in on what I need.

I have two date variables let's call them date1 date2.  they are strings always in the format of mm-dd-yyyy.    All I want to do is determine the number of days between these two dates.  I'm pretty sure I first need to convert these to another format but I'm not sure how to code this... can someone show me the javascript code to do this?

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.


// get the parts of the date

// rebuild the date so js understands it and convert to millisecs
date1=new Date(date1[2], date1[0] - 1, date1[1]).getTime();
date2=new Date(date2[2], date2[0] - 1, date2[1]).getTime();

// difference between the dates in millisecs
var difference = Math.abs(date1 - date2)

// divide result by number of millisecs in a day (rounded to a whole)

Open in new window

Dan CraciunIT ConsultantCommented:
Use gettime() on your dates:

(date1.getTime() - date2.getTime() )/ (24*60*60*1000)

will give you the number of days between the dates

aviator21114Author Commented:
GaryC123.... what's happening on lines 10 and 11 with the subscripts?  Date1(0) -1 can you define this construct?
Dan CraciunIT ConsultantCommented:
Your months are in range 1-12 and JS needs them in the range 0-11.
It's building the date as a date object 2013,11,01 - this is how js will understand it.

The 11 is not a mistake - months in js start from 0 to 11

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
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

From novice to tech pro — start learning today.