3

例:

  • 今は2012年3月26日です。来年の2月にお願いします。2013-01-01を返す必要があります
  • 今は2012年3月26日です。来年の4月にお願いします。2012-04-01を返す必要があります
4

3 に答える 3

5

あなたはこのようなことをすることができます。

// date is a JS date or moment
// month is the zero indexed month (0 - 11)
function nextMonth(date, month) {
    var input = moment(date);
    var output = input.clone().startOf('month').month(month);
    return output > input ? output : output.add(1, 'years');
}

瞬間の操作に関するドキュメントを参照してください。http://momentjs.com/docs/#/manipulating/

于 2012-12-26T22:46:51.757 に答える
2

これを書いた:

    /**
        @var date is a JS date or moment
        @var month is the month in the 0-11 format
     */
    var getNextMonthOccurrence: function(date, month){
        var m = moment(date);

        var this_year = new Date(m.year(), month, 1);
        var next_year = new Date(m.year() + 1, month, 1);

        return this_year > m ? this_year : next_year;
    }

しかし、それを行うためのより良い方法が必要です...

于 2012-12-26T23:13:30.360 に答える
1

来年1月1日: moment().month(0+12).date(1).hour(0).minute(0).second(0)

来年の3月17日: moment().month(2).date(17).hour(0).minute(0).second(0)

編集:作成された日付が現在よりも短いかどうかに注意を払う必要があります。現在2月なので、次の1月を取得するには12か月追加する必要がありますが、次の3月を取得する場合は追加されません。

function getNextJan(){
    var j = moment().month(0).date(1).hour(0).minute(0).second(0)
    if(j < moment()) return j.month(12)
    return j
}
于 2015-02-22T20:50:02.433 に答える