Search code examples
javascriptjquerydateelapsed

Convert 'Month and Year' string into Date object using JS and Jquery


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:

  1. Is there anyway to do this without changing it to a date object, since I'm not interested in the days or time?

  2. 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?

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


Solution

  • Really, all you need is a map from month names to numbers. You can build one like this:

    var monthNames = [ "January",   "February", "March",    "April",
                       "May",       "June",     "July",     "August",
                       "September", "October",  "November", "December" ]
    var monthNumber = {}
    for (var i=0; i<monthNames.length; ++i) {
        monthNumber[monthNames[i]] = i;
    }
    

    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.

    function stringToMonthNumber(monthYear) {
        var parts = monthYear.split(/\s*,\s*/)
        var month = monthNumber[parts[0]]
        var year = parts[1] - 1
        return year * 12 + month
    }
    
    function elapsedMonths(startString, endString) {
        return stringToMonthNumber(endString) - stringToMonthNumber(startString)
    }
    

    Then this:

    elapsedMonths("November, 2011", "June, 2012")
    

    returns 7.