【问题标题】:Get all days of the months from specific day using momentJS in Angular 6使用Angular 6中的momentJS从特定日期获取月份的所有日期
【发布时间】:2018-11-25 20:04:38
【问题描述】:

我试图创建一个“静态”自定义日期选择器,它将在日历视图中显示该月的所有日期,我想显示所选月份从星期日到星期六的所有日期。我现在得到的结果是从 1 日到当月的所有日期

  getDaysArrayByMonth() {
    this.monthSwitched = !this.monthSwitched;
    this.daysOfSelectedMonth = [];
    this.monthDaysAmount = moment(this.selectedMonth, 'MMMM YYYY').daysInMonth();
    const remainingToEvenRows = 35 - this.monthDaysAmount;
    const days = this.monthDaysAmount + remainingToEvenRows;
    for (let i = 0; i < days; i++) {
      this.daysOfSelectedMonth.push(moment(this.selectedMonth, 'MMMM YYYY').month(this.selectedMonth).date(i + 1).format('l'));
    }
  }

【问题讨论】:

    标签: javascript angular momentjs


    【解决方案1】:
                function getDaysInMonth(date) {
                    date = date.startOf('month');
                    let month = date.month();
                    let days = [];
                    while (date.month() === month) {
                        days.push(moment(date));
                        date.add(1, 'days')
                    }
                    return days;
                }
    

    【讨论】:

      猜你喜欢
      • 2017-01-19
      • 2014-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-17
      • 2021-10-17
      相关资源
      最近更新 更多