I have two arrays ( i made them from jQuery from a table), the first is the end date, and the second is the start date. The elements are strings:
["June, 2012", "June, 2012", "August, 2011", "April, 2013", "August, 2010", "August, 2010", "April, 2013", "April, 2012", "April, 2012", "June, 2012", "June, 2012", "June, 2012", "June, 2012", "June, 2012", "June, 2012", "June, 2012", "June, 2012", "June, 2012", "June, 2012", "June, 2012", "June, 2012", "June, 2012", "June, 2012", "June, 2012", "June, 2012", "June, 2012", "June, 2012", "June, 2012"]
["November, 2011", "April, 2012", "May, 2008", "May, 2007", "November, 2007", "May, 2007", "June, 2006", "June, 2007", "April, 2006", "January, 2008", "April, 2001", "April, 2001", "April, 2006", "April, 1998", "April, 1998", "September, 2008", "August, 2010", "August, 2009", "August, 2010", "August, 2009", "August, 2010", "August, 2010", "August, 2010", "January, 1997", "January, 1997", "January, 2010", "January, 2007", "April, 2010"]
I am trying to get the elapsed time between each set of indices. I would assume that I have to convert these strings to a Date Object date() then do the calculation to get the elapsed time, then truncated it to just the month and year using something like this:
function convertDate(passedDate) {
var m = new Array(7);
var y = passedDate.getFullYear();
m[0] = "January";
m[1] = "February";
m[2] = "March";
m[3] = "April";
m[4] = "May";
m[5] = "June";
m[6] = "July";
m[7] = "August";
m[8] = "September";
m[9] = "October";
m[10] = "November";
m[11] = "December";
return m[passedDate.getMonth()] + "months and " + y + " years;
};
Here is the fiddle and here are my questions:
-
Is there anyway to do this without changing it to a date object, since I’m not interested in the days or time?
-
Is there another approach you would suggest than trying to convert to date for the elapsed date math formula, then converting back to a string?
-
How would I identify the sister elements of the current element, so that I could avoid using a double nested loop? ( will also ask this in a different question since it addresses a different topic)
Thanks for helping me, as I’m trying to make an interactive resume with selectable attributes, and I’m including some data so I don’t have to answer all the basic information to recruiters who cold call.
Really, all you need is a map from month names to numbers. You can build one like this:
Now you have, e.g.,
monthNumber['October']== 9. Then you can turn one of your strings into a month number and year. If you then turn that into an absolute month number (year * 12 + month), you can just subtract to get the elapsed months.Then this:
returns 7.