简体   繁体   English

在下拉列表中控制月份年份

[英]Control Month Year in Dropdown list

I am trying to control "Month" and "Year" in drop down list.我试图在下拉列表中控制“月”和“年”。 I am having two fields, "Start Date" and "End Date".我有两个字段,“开始日期”和“结束日期”。 In that "Start Date" I am listing the "Current Month" and "Year" like "March 2020".在那个“开始日期”中,我列出了“本月”和“年”,例如“2020 年 3 月”。 I want to control my "End Date" based of "Start Date".我想根据“开始日期”控制我的“结束日期”。 For example if there is no "Month" selected in "Start Date" I should not allow user to select the "End Date".例如,如果在“开始日期”中没有选择“月份”,我就不应该允许用户选择“结束日期”。 If user select's "MAY 2020" in "Start Date" and in "End Date" I want to display from "May 2020 to next May 2021".如果用户在“开始日期”和“结束日期”中选择“2020 年 5 月”,我想显示“2020 年 5 月至 2021 年 5 月”。 Like wise if user select's "June 2020" in "Start Date" and in "End Date" I want to display from "June 2020 to next June 2021".同样,如果用户在“开始日期”和“结束日期”中选择“2020 年 6 月”,我想从“2020 年 6 月到下一个 2021 年 6 月”显示。 As of now I am displaying the current month and year in both Start and End date.截至目前,我在开始和结束日期中显示当前月份和年份。 Any one can guide me to achieve this.任何人都可以指导我实现这一目标。 Thanks in Advance.提前致谢。 Below is the code where I am getting current year month to next year.下面是我从当前月份到明年的代码。

const fareMon = () => {
    const monthList = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
    const currDate = new Date();
    const year = currDate.getFullYear();
    const months = [];
    let currentMonthIndex = currDate.getMonth();
    let yearsToAdd = 0;
    for (let i = 0; i < 13; i += 1) {
      if (currentMonthIndex === 12) {
        currentMonthIndex = 0;
        yearsToAdd += 1;
      }
      const futYear = year + yearsToAdd;
      months.push(<option value={`${monthList[currentMonthIndex]} ${futYear}`}>{`${monthList[currentMonthIndex]} ${futYear}`}</option>);
      currentMonthIndex += 1;
    }

    return <>{months}</>;
  };  

So, what I have done is, created a getFullYear function, where you can pass year and you will get all the months所以,我所做的是,创建了一个getFullYear函数,在那里你可以通过一年,你会得到所有的月份

Does this help:这是否有帮助:

const monthList = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];

const getFullYear = (year) => {
  const currentDate = new Date(`01 01 ${year}`);
  const monthYear = [];
  for(;currentDate.getFullYear() === year;){
    const currentMonth = currentDate.getMonth();
    monthYear.push(`${monthList[currentMonth]} ${year}`)
    const nextMonth = currentMonth+1;
    currentDate.setMonth(nextMonth);
  }
  return monthYear;
}

More cleaner and less compute intensive approach would be:更清洁、计算密集度更低的方法是:

const monthList = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];

const getYear = (year) => {
  return monthList.map((month) => `${month} ${year}`);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM