# Get the number of days in a given month

Read day 0 of the next month for a 1-based month.

Canonical: https://1loc.dev/date-time/get-the-number-of-days-in-given-month/
Version: 1.0.0
Runtime: JavaScript · Node.js 22 / modern browsers
Verification: Local fixtures; no external services
Updated: 2026-10-28

## Implementation

```javascript
function daysInMonth({ year, month }) { const d = new Date(0); d.setUTCFullYear(year, month, 0); return d.getUTCDate(); }
```

## Input

```json
{
  "year": 2024,
  "month": 2
}
```

## Expected output

```json
29
```

## Edge case

Input:
```json
{
  "year": 2026,
  "month": 2
}
```
Output:
```json
28
```

## Limits

- The original comment called the month zero-based, but new Date(year, month, 0) gives the length of a 1-based month; this page says so explicitly.

The lab validates types and size before calling this function. Add equivalent validation at your own trust boundary.

## Source and license

Original: https://github.com/phuocng/1loc/blob/14b481cf60dff1620ab1bb99a43b1f68068cdd9b/contents/get-the-number-of-days-in-given-month.mdx
Author: Nguyen Huu Phuoc and original contributors
Contributors: See original repository history
Source commit: 14b481cf60dff1620ab1bb99a43b1f68068cdd9b
Copyright (c) 2020 phuoc-ng
License: MIT — https://1loc.dev/licenses/
Maintainer: Oveyon
Modifications: Uses an explicit 1-based month in UTC; the original comment called the month zero-based although the code used 1-based months. Original MDX retained as inert source evidence.
