如何获得上一年的月份名称?

时间:2018-05-30 07:14:12

标签: javascript arrays

我被困在JavaScript中。我希望达到预期的效果。

这是我的代码。

Javascript代码



// Months between years.
const monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
    "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
];
let joiningDate = new Date("08-08-2017");
let currentDate = new Date();
var months = (currentDate.getFullYear() - joiningDate.getFullYear()) * 12;

// Months between... months.
months += currentDate.getMonth() - joiningDate.getMonth();
var afterDec = currentDate.setDate(12);

if (joiningDate.getDate() < currentDate.getDate()) {
    months--;
}

var month = currentDate.getMonth();
for (var i = 0; i < months; i++) {
    if (month > 0) {
        month = currentDate.getMonth();
        console.log(monthNames[month - i]);
    }
}
&#13;
&#13;
&#13;

  

控制台上的实际结果

10:48:42.032 points.ts:109 May
10:48:42.033 points.ts:109 Apr
10:48:42.034 points.ts:109 Mar
10:48:42.035 points.ts:109 Feb
10:48:42.037 points.ts:109 Jan
10:48:42.038 points.ts:109 undefined
10:48:42.039 points.ts:109 undefined
10:48:42.039 points.ts:109 undefined
  

控制台上的预期结果

10:48:42.032 points.ts:109 May
10:48:42.033 points.ts:109 Apr
10:48:42.034 points.ts:109 Mar
10:48:42.035 points.ts:109 Feb
10:48:42.037 points.ts:109 Jan
10:48:42.038 points.ts:109 Dec
10:48:42.039 points.ts:109 Nov
10:48:42.039 points.ts:109 Oct

如何添加达到预期结果?

提前感谢。

3 个答案:

答案 0 :(得分:2)

使用modulo环绕到所需的月份。因为本机模数可以返回一个负数(不能用于直接在数组中查找索引),所以定义一个mod函数,它将返回一个正(或0)索引:

&#13;
&#13;
const mod = (x, n) => (x % n + n) % n;
const monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
];
let joiningDate = new Date("08-08-2017");
let currentDate = new Date();
var months = (currentDate.getFullYear() - joiningDate.getFullYear()) * 12;

// Months between... months.
months += currentDate.getMonth() - joiningDate.getMonth();
var afterDec = currentDate.setDate(12);

if (joiningDate.getDate() < currentDate.getDate()) {
  months--;
}

var month = currentDate.getMonth();
for (var i = 0; i < months; i++) {
  if (month > 0) {
    month = currentDate.getMonth();
    console.log(monthNames[mod(month - i, monthNames.length)]);
  }
}
&#13;
&#13;
&#13;

答案 1 :(得分:0)

如果我理解了代码的逻辑,我认为你可以使用接受的答案中的模数来简化:

const mod = (x, n) => (x % n + n) % n;

const monthDiff = (d1, d2) =>
    ((d2.getFullYear() - d1.getFullYear()) * 12) - 
    (d1.getMonth() + 1) + 
    (d2.getMonth())

  const monthsNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
      "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]; 
  const joiningDate = new Date("2017-08-08");
  const currentDate = new Date(); 
  const currentMonth = currentDate.getMonth();
  const monthsCount = monthDiff(joiningDate, currentDate)

  for (let i = 0; i < monthsCount; i++) {
    console.log(monthsNames[mod(currentMonth - i, 12)])
  }

答案 2 :(得分:0)

您还可以使用recursive功能获得所需的结果。

的步骤

  1. 以递归方式传递当前日期
  2. 获取通过日期的第一天。
  3. 根据要求检查条件是否
  4. 如果是setDate(-1),那么您将从过去的日期开始上个月。
  5. <强>样本

    const monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
        joiningDate = new Date("08-08-2017"),
        jMonth = joiningDate.getMonth() + 1,
        jyear = joiningDate.getFullYear();
    
    function previousMonths(date) {
        let month = date.getMonth(),
            year = date.getFullYear();
    
        date = new Date(year, month, 1);
        /*
         * First condition based on year beucase date me be changed for Example {new Date("08-08-2016")} so first we come upto year same
         * second condtion based on month upto whatever we need to show
         */
        if (jyear != year || jMonth != month) {
            console.log(monthNames[month]); // get the value form month array bassed on {month}
            previousMonths(new Date(date.setDate(-1))); //Again call {reviousMonths()} by passing -1 in date
        }
    }
    previousMonths(new Date());
    .as-console-wrapper {max-height: 100% !important;top: 0;}