momentjs get if date is this week, this month or this year

后端 未结 2 1195
面向向阳花
面向向阳花 2021-01-18 04:55

I am trying to pass random date values to moment and check if the date is this week, this month or this year but it seems there is no simple function to achive this without

2条回答
  •  天命终不由人
    2021-01-18 05:33

    Use .isSame()

    Check if a moment is the same as another moment.

    moment().isSame(Moment|String|Number|Date|Array); moment().isSame(Moment|String|Number|Date|Array, String);

    Supported units: year month week day hour minute second

    moment('2010-10-20').isSame('2010-01-01', 'year');  // true
    moment('2010-01-01').isSame('2011-01-01', 'month'); // false, different year
    moment('2010-01-01').isSame('2010-02-01', 'day');   // false, different month
    

提交回复
热议问题