Need to know how many days are in a specific month? This guide shows you how to use the Days in Month API to get accurate day counts for any month in any year, with automatic leap year handling.
Why Calculate Days in Month?
The number of days in a month varies: 28, 29, 30, or 31 days depending on the month and year. February is particularly tricky with its leap year variation. Using an API ensures you always get the correct count without implementing complex logic.
This is crucial for billing systems, calendar applications, and any feature that needs to work with month boundaries.
Understanding Leap Years
A year is a leap year if:
- It is divisible by 4, AND
- It is NOT divisible by 100, UNLESS
- It is also divisible by 400
For example: 2024 is a leap year, 2100 is not, but 2000 was.
Using the Days in Month API
TinyFn provides a simple endpoint to get days in any month:
GET https://api.tinyfn.io/v1/time/days-in-month
Headers: X-API-Key: your-api-key
{
"year": 2024,
"month": 2,
"month_name": "February",
"days": 29,
"is_leap_year": true
}
Parameters
| Parameter | Type | Description |
|---|---|---|
year |
integer | Year (e.g., 2024). Required. |
month |
integer | Month number 1-12 (e.g., 2 for February). Required. |
Alternatively, you can pass a date:
| Parameter | Type | Description |
|---|---|---|
date |
string | Date in ISO 8601 format - extracts year and month from this |
Code Examples
JavaScript / Node.js
const response = await fetch(
'https://api.tinyfn.io/v1/time/days-in-month?' + new URLSearchParams({
year: 2024,
month: 2
}),
{ headers: { 'X-API-Key': 'your-api-key' } }
);
const result = await response.json();
console.log(result.days); // 29 (2024 is a leap year)
console.log(result.is_leap_year); // true
Python
import requests
response = requests.get(
'https://api.tinyfn.io/v1/time/days-in-month',
params={'year': 2023, 'month': 2},
headers={'X-API-Key': 'your-api-key'}
)
result = response.json()
print(result['days']) # 28 (2023 is not a leap year)
print(result['is_leap_year']) # False
cURL
curl "https://api.tinyfn.io/v1/time/days-in-month?year=2024&month=12" \
-H "X-API-Key: your-api-key"
Common Use Cases
- Billing Systems: Calculate pro-rated charges for partial months
- Calendar Apps: Display correct number of day cells
- Date Pickers: Limit date selection to valid days
- Scheduling: Validate dates before saving
- Reports: Calculate monthly averages accurately
Best Practices
- Cache results: Days in month for past dates never change
- Validate input: Ensure month is 1-12 before calling the API
- Handle edge cases: Plan for the varying February length in your logic
- Consider timezones: The current month may differ by timezone
Try the Days in Month API
Get your free API key and simplify your calendar calculations.
Get Free API Key