首页 > 解决方案 > 我可以在时刻 js 中得到今天的日期加上一个月吗?

问题描述

我有一个获取今天日期的 ion-datetime 输入。我需要在今天后一个月动态设置最大日期。我可以使用 moment.js 实现这一点吗?

这是我的输入:

<ion-datetime display-format="MMMM YYYY" max=" ONE MONTH AFTER TODAY??"></ion-datetime>

标签: angularionic-frameworkmomentjs

解决方案


<ion-datetime displayFormat="MMMM YY" min="{{min}}" max="{{max}}"></ion-datetime>

现在您需要最小值和最大值。

首先,让我们找出今天和下个月。

const today     = moment(new Date()).format('YYYY-MM-DD');

const nextMonth = moment(today).add(1, 'M');
var nextMonthEnd = moment(nextMonth).endOf('month');

if(today.date() != nextMonth.date() && nextMonth.isSame(nextMonthEnd.format('YYYY-MM-DD'))) {
    nextMonth = nextMonth.add(1, 'd');
}

console.log(today.format('DD-MM-YYYY'));
console.log(nextMonth.format('DD-MM-YYYY'));

现在

this.min = this.today;

this.max = this.nextMonth

推荐阅读