获取2个日期之间的月份名称

时间:2014-01-29 20:22:24

标签: javascript jquery

我必须有两个日期fromto。我希望在这两个日期之间获得所有月份名称。

以下是我的代码

var monthNames = [ "January", "February", "March", "April", "May", "June",
        "July", "August", "September", "October", "November", "December" ];

function diff(from, to) {
    var datFrom = new Date('1 ' + from);
    var datTo = new Date('1 ' + to);
    var arr = monthNames.slice(datFrom.getMonth(), datTo.getMonth() + 1);
}

以上代码适用于以下输入

diff('September 2013', 'December 2013');

但它不适用于此

diff('September 2013', 'February 2014');

我怎样才能让它发挥作用?

5 个答案:

答案 0 :(得分:6)

我的更好:http://jsfiddle.net/kS73f/8/

var monthNames = [ "January", "February", "March", "April", "May", "June",
        "July", "August", "September", "October", "November", "December" ];

function diff(from, to) {
    var arr = [];
    var datFrom = new Date('1 ' + from);
    var datTo = new Date('1 ' + to);
    var fromYear =  datFrom.getFullYear();
    var toYear =  datTo.getFullYear();
    var diffYear = (12 * (toYear - fromYear)) + datTo.getMonth();

    for (var i = datFrom.getMonth(); i <= diffYear; i++) {
        arr.push(monthNames[i%12] + " " + Math.floor(fromYear+(i/12)));
    }        

    return arr;
}

console.log(diff('September 2013', 'March 2014'));

答案 1 :(得分:2)

你将不得不做一个比切片更多的手动方法。这是一个起点,您可以确定如何处理案例中提到的案例。

function diff(from, to) {
    var result = [];
    var datFrom = new Date('1 ' + from);
    var datTo = new Date('1 ' + to);
    if(datFrom < datTo) {
      var month = datFrom.getMonth();
      var toMonth = datTo.getMonth() + 1 + ((datTo.getYear() - datFrom.getYear())*12); //toMonth adjusted for year
      for(; month < toMonth; month++) { //Slice around the corner...
        result.push(monthNames[month % 12]);
      }
    }

    return result;
}

diff('September 2013', 'February 2014'); //=["September", "October", "November", "December", "January", "February"]

答案 2 :(得分:0)

var monthNames = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ];

function diff(from, to) {
    var datFrom = new Date('1 ' + from);
    var datTo = new Date('1 ' + to);
    var arr;
    if(datFrom > datTo) {
      return diff(to, from);
    }

    var fromYear = datFrom.getFullYear();
    var toYear = datTo.getFullYear();

    if(fromYear === toYear) {
      return monthNames.slice(datFrom.getMonth(), datTo.getMonth() + 1);
    } else {
      var arr = addYear(monthNames.slice(datFrom.getMonth(), new Date('1 December ' + fromYear)), fromYear);
      for(var i = 1; i < (toYear - fromYear); i++) {
        arr = arr.concat(addYear(monthNames, fromYear + i));
      }
      return arr.concat(addYear(monthNames.slice(new Date('1 January ' + fromYear).getMonth(), datTo.getMonth() + 1), toYear));
    }
}

function addYear(arr, year) {
  var updatedArr = [];
  for(var i = 0; i < arr.length; i++) {
    updatedArr[i] = arr[i] + ' ' + year;
  }
  return updatedArr;
}

尝试console.log(diff('September 2013', 'February 2015'))来测试它。

答案 3 :(得分:0)

如果从理解的角度帮助OP,以下内容会尽可能少地修改原始函数。

function diff(from, to) {
    var datFrom = new Date('1 ' + from);
    var datTo = new Date('1 ' + to);
    var arr = monthNames.slice(datFrom.getMonth(), datTo.getMonth() + 1);

    if (!arr.length) {
        arr = monthNames.slice(datFrom.getMonth(), 12);
        arr = arr.concat(monthNames.slice(0, datTo.getMonth() + 1));
    }

    return arr;
}

console.log(diff('December 2013', 'February 2014')); //["September", "October", "November", "December", "January", "February"]

答案 4 :(得分:-1)

懒惰回答:

var monthNames = [ "January", "February", "March", "April", "May", "June",
        "July", "August", "September", "October", "November", "December", 
        "January", "February", "March", "April", "May", "June",
        "July", "August", "September", "October", "November", "December" ];

function diff(from, to) {
    var mFrom = new Date('1 ' + from).getMonth();
    var mTo = new Date('1 ' + to).getMonth();
    mTo = mTo < mFrom ? mTo + 12 : mTo;
    return monthNames.slice(mFrom, mTo + 1);
}

alert(diff('September 2013', 'December 2013'));
alert(diff('September 2013', 'February 2014'));