【问题标题】:Get days of the month of specific year with - moment JS and Angular使用 - moment JS 和 Angular 获取特定年份的月份中的天数
【发布时间】:2018-11-17 22:07:51
【问题描述】:

我试图在 Moment JS 的帮助下以角度创建自定义日期选择器。 当年份变化时我无法获得正确月份的日期的问题

getDaysArrayByMonth() 仅返回 2018 年的月份

  public selectedMonth: any
  private monthIterator = 0
  public daysOfSelectedMonth = [];
  public monthDaysAmount: number

  onNextMonth() {
    this.monthIterator++
    this.selectedMonth = moment().add(this.monthIterator, 'months').format('MMMM YYYY');
    this.getDaysArrayByMonth()
  }

  onPreviousMonth() {
    this.monthIterator--
    this.selectedMonth = moment().add(this.monthIterator, 'months').format('MMMM YYYY');
    this.getDaysArrayByMonth()

  }

  getDaysArrayByMonth(){
    this.daysOfSelectedMonth = []
    this.monthDaysAmount = moment().month(this.selectedMonth).daysInMonth() ;
    const remainingToEvenRows = 35 - this.monthDaysAmount
    const days = this.monthDaysAmount + remainingToEvenRows

    for (let i = 0; i < days; i++) {
      this.daysOfSelectedMonth.push(moment().month(this.selectedMonth).date(i + 1).format('Do MMMM YYYY'));
    }
  }

【问题讨论】:

    标签: javascript angular momentjs


    【解决方案1】:

    this.selectedMonth 正在保存月份和年份,因此您应该在 moment 构造函数中使用结果:

    moment(this.selectedMonth, 'MMMM YYYY')

    this.monthDaysAmount = moment(this.selectedMonth, 'MMMM YYYY').daysInMonth() ;
    for (let i = 0; i < days; i++) {
      this.daysOfSelectedMonth.push(moment(this.selectedMonth, 'MMMM YYYY').date(i + 1).format('Do MMMM YYYY'));
    }
    

    【讨论】:

      【解决方案2】:

      moment() 函数始终返回当前日期,因此如果您在其上构建逻辑,它将始终返回当前月份和年份。所以你必须在构造函数中指定年份和月份才能返回所需的/过去的月份。

      所以把这段代码改成如下

      /// public selectedMonth: any
      private monthIterator = 0
      public daysOfSelectedMonth = [];
      public monthDaysAmount: number
      
      onNextMonth() {
      this.monthIterator++
      /// this.selectedMonth = moment().add(this.monthIterator, 'months').format('MMMM YYYY');
      this.getDaysArrayByMonth(moment("201501", "YYYYMM").add(1, 'months').format('YYYYMM'));
      }
      
      onPreviousMonth() {
      this.monthIterator--
      /// this.selectedMonth = moment().add(this.monthIterator, 'months').format('MMMM YYYY');
      
      this.getDaysArrayByMonth(moment("201501", "YYYYMM").add(1, 'months').format('YYYYMM'))
      }
      
      getDaysArrayByMonth(yearMonth: string){
      this.daysOfSelectedMonth = []
      /// this.monthDaysAmount = moment().month(this.selectedMonth).daysInMonth() ;
      this.monthDaysAmount = moment(yearMonth,'YYYYMM').daysInMonth() ;
      const remainingToEvenRows = 35 - this.monthDaysAmount
      const days = this.monthDaysAmount + remainingToEvenRows
      
      for (let i = 0; i < days; i++) {
        this.daysOfSelectedMonth.push(moment().month(this.selectedMonth).date(i + 1).format('Do MMMM YYYY'));
      }
      

      }

      请删除硬编码的201501 并将YearMonth 组合传递给函数onNextMonthonPreviousMonth

      查看stackblitz

      【讨论】:

        猜你喜欢
        • 2012-02-14
        • 1970-01-01
        • 2018-03-27
        • 2013-06-23
        • 2017-12-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多