You-Dont-Need-Momentjs icon indicating copy to clipboard operation
You-Dont-Need-Momentjs copied to clipboard

add a note to native Days in Month

Open cymen opened this issue 5 years ago • 5 comments

Not sure on the exact wording but native days in month is confusing because the second parameter to Date is normally zero-based month however in this case, we need the number for the month after the desired month. In other words, December === 11 with zero-based but if we want number of days in December, we have to pass in 12.

Example:

new Date(2018, 11, 0).getDate()
> 30
new Date(2018, 12, 0).getDate()
> 31

cymen avatar Dec 20 '18 06:12 cymen

Pull Request Test Coverage Report for Build 169

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 100.0%

Totals Coverage Status
Change from base Build 168: 0.0%
Covered Lines: 43
Relevant Lines: 43

💛 - Coveralls

coveralls avatar Dec 20 '18 06:12 coveralls

Pull Request Test Coverage Report for Build 169

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 100.0%

Totals Coverage Status
Change from base Build 168: 0.0%
Covered Lines: 43
Relevant Lines: 43

💛 - Coveralls

coveralls avatar Dec 20 '18 06:12 coveralls

Thanks @cymen. The reason for this is day is 1-based. So if you supply 0, it's the same as the last day of last month. I'd add a description at the top stating we are checking how many days in Feb 2012. And perhaps adding a comment in date-fns to mention this is special.

stevemao avatar Dec 20 '18 09:12 stevemao

@stevemao I didn't find the day part confusing -- it's the month or second parameter to new Date(). If I want the months for December (11 in zero-based), I need to pass in 12 (or the month "after" what I am looking for).

cymen avatar Dec 20 '18 16:12 cymen

Yeah, because new Date(2012, 02, 0) is the same as new Date(2012, 01, 29)

stevemao avatar Dec 20 '18 22:12 stevemao