最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Convert 'Month and Year' string into Date object using JS and Jquery - Stack Overflow

programmeradmin4浏览0评论

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.

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.

Share Improve this question edited Jun 10, 2012 at 21:28 chris Frisina asked Jun 10, 2012 at 20:31 chris Frisinachris Frisina 19.7k23 gold badges91 silver badges172 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

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.

发布评论

评论列表(0)

  1. 暂无评论