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

Posted on 2013-09-22
Medium Priority
Last Modified: 2013-09-22
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?

Question by:aviator21114
  • 2
  • 2
LVL 58

Expert Comment

ID: 39513244

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

LVL 35

Expert Comment

by:Dan Craciun
ID: 39513246
Use gettime() on your dates:

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

will give you the number of days between the dates


Author Comment

ID: 39513260
GaryC123.... what's happening on lines 10 and 11 with the subscripts?  Date1(0) -1 can you define this construct?
LVL 35

Expert Comment

by:Dan Craciun
ID: 39513268
Your months are in range 1-12 and JS needs them in the range 0-11.
LVL 58

Accepted Solution

Gary earned 2000 total points
ID: 39513271
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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question