Dayjs: .add(1, 'month') Display is wrong

Created on 20 Oct 2018  路  1Comment  路  Source: iamkun/dayjs

dayjs('2018/2/28').add(1,'month').format('YYYY/MM/DD') => 2018/3/28
but i want 2018/3/31
dayjs('2018/4/30').add(1,'month').format('YYYY/MM/DD') => 2018/5/30
but i want 2018/5/31

Is that a bug ?

Most helpful comment

The add method adds a calendar month. Not 30 or 31 days to move the date always to the end of the month. The method add cannot know, which one of the two options you want :-) If you want the end of the month, have a look at endOf:

dayjs('2018/2/28').add(1,'month').endOf('month')
dayjs('2018/4/30').add(1,'month').endOf('month')

>All comments

The add method adds a calendar month. Not 30 or 31 days to move the date always to the end of the month. The method add cannot know, which one of the two options you want :-) If you want the end of the month, have a look at endOf:

dayjs('2018/2/28').add(1,'month').endOf('month')
dayjs('2018/4/30').add(1,'month').endOf('month')
Was this page helpful?
0 / 5 - 0 ratings

Related issues

paraself picture paraself  路  3Comments

antony picture antony  路  5Comments

fundon picture fundon  路  5Comments

axelg12 picture axelg12  路  4Comments

Sunrise1970 picture Sunrise1970  路  4Comments