cal_days_in_month

cal_days_in_month(int $calendar, int $month, int $year): int

指定历法下某年某月的天数(calendar 扩展):CAL_GREGORIAN 配公历闰年规则——4 除尽且非 100 整除、或 400 整除则 2 月 29 天;2024-02=29 与 2023-02=28 的对比是闰年教学的标准样本,2000/1900 两例补齐世纪年规则。

C C23

C23
/* libc 无历法 API:公历规则就是一个分支——4 规则、100 规则、400 规则 */
#include <stdio.h>

static int days_in(int y, int m) {
    static const int d[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    if (m == 2 && ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0)) {
        return 29;
    }
    return d[m - 1];
}

int main(void) {
    printf("%d\n", days_in(2024, 2)); /* 29——闰年 */
    printf("%d\n", days_in(2023, 2)); /* 28 */
    printf("%d\n", days_in(2000, 2)); /* 29——400 规则胜出 */
    printf("%d\n", days_in(1900, 2)); /* 28——100 规则生效 */
    printf("%d %d\n", days_in(2024, 1), days_in(2024, 4)); /* 31 30 */
    return 0;
}
输出
29
28
29
28
31 30

C++ C++26

C++26
#include <iostream>

// std::chrono 的 year_month_day_last{y/m/last}.day() 是 C++20 的类型化答案,
// 但本工具链的 libc++ 未启用历法组——手写公历规则
// (GCC 的 libstdc++ 可用)
static int days_in(int y, int m) {
    static const int d[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    if (m == 2 && ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0)) {
        return 29;
    }
    return d[m - 1];
}

int main() {
    std::cout << days_in(2024, 2) << "\n"; // 29——闰年
    std::cout << days_in(2023, 2) << "\n"; // 28
    std::cout << days_in(2000, 2) << "\n"; // 29——400 规则胜出
    std::cout << days_in(1900, 2) << "\n"; // 28——100 规则生效
    std::cout << days_in(2024, 1) << " " << days_in(2024, 4) << "\n"; // 31 30
    return 0;
}
输出
29
28
29
28
31 30

C# v11.0.100

v11.0.100
using System;

// DateTime.DaysInMonth:一一对应的静态方法(只有公历,无历法参数)
Console.WriteLine(DateTime.DaysInMonth(2024, 2)); // 29——闰年
Console.WriteLine(DateTime.DaysInMonth(2023, 2)); // 28
Console.WriteLine(DateTime.DaysInMonth(2000, 2)); // 29——400 规则胜出
Console.WriteLine(DateTime.DaysInMonth(1900, 2)); // 28——100 规则生效
Console.WriteLine(DateTime.DaysInMonth(2024, 1) + " " + DateTime.DaysInMonth(2024, 4)); // 31 30
输出
29
28
29
28
31 30

Go v1.27.4

v1.27.4
package main

import (
	"fmt"
	"time"
)

// 没有现成 DaysInMonth:m+1 月的第 0 天就是 m 月最后一天
func daysIn(y int, m time.Month) int {
	return time.Date(y, m+1, 0, 0, 0, 0, 0, time.UTC).Day()
}

func main() {
	fmt.Println(daysIn(2024, time.February)) // 29——闰年
	fmt.Println(daysIn(2023, time.February)) // 28
	fmt.Println(daysIn(2000, time.February)) // 29——400 规则胜出
	fmt.Println(daysIn(1900, time.February)) // 28——100 规则生效
	fmt.Println(daysIn(2024, time.January), daysIn(2024, time.April)) // 31 30
}
输出
29
28
29
28
31 30

Java v27.0.1

v27.0.1
import java.time.YearMonth;

public class Main {
    public static void main(String[] args) {
        // YearMonth.lengthOfMonth():一一对应(闰年规则内建)
        System.out.println(YearMonth.of(2024, 2).lengthOfMonth()); // 29——闰年
        System.out.println(YearMonth.of(2023, 2).lengthOfMonth()); // 28
        System.out.println(YearMonth.of(2000, 2).lengthOfMonth()); // 29——400 规则胜出
        System.out.println(YearMonth.of(1900, 2).lengthOfMonth()); // 28——100 规则生效
        System.out.println(YearMonth.of(2024, 1).lengthOfMonth() + " " + YearMonth.of(2024, 4).lengthOfMonth()); // 31 30
    }
}
输出
29
28
29
28
31 30

JavaScript v26.13.1

v26.13.1
// m+1 月的第 0 天就是 m 月最后一天(此处参数 m 按人类 1-12 习惯)
const daysIn = (y, m) => new Date(y, m, 0).getDate();
console.log(daysIn(2024, 2)); // 29——闰年
console.log(daysIn(2023, 2)); // 28
console.log(daysIn(2000, 2)); // 29——400 规则胜出
console.log(daysIn(1900, 2)); // 28——100 规则生效
console.log(daysIn(2024, 1), daysIn(2024, 4)); // 31 30
输出
29
28
29
28
31 30

PHP v8.6.0

v8.6.0
// cal_days_in_month(历法, 月, 年):calendar 扩展的月天数——
// 四条断言讲完闰年
var_dump(cal_days_in_month(CAL_GREGORIAN, 2, 2024)); // 29:被 4 整除且不被 100 整除
var_dump(cal_days_in_month(CAL_GREGORIAN, 2, 2023)); // 28:平年常态
var_dump(cal_days_in_month(CAL_GREGORIAN, 2, 2000)); // 29:400 规则胜出
var_dump(cal_days_in_month(CAL_GREGORIAN, 2, 1900)); // 28:100 规则生效
echo cal_days_in_month(CAL_GREGORIAN, 1, 2024), "/", cal_days_in_month(CAL_GREGORIAN, 4, 2024), "\n"; // 1 月 31 天,4 月 30 天
输出
int(29)
int(28)
int(29)
int(28)
31/30

Python v3.15.2

v3.15.2
import calendar

# calendar.monthrange(y, m)[1]:一一对应(元组第二槽 = 天数)
print(calendar.monthrange(2024, 2)[1])  # 29——闰年
print(calendar.monthrange(2023, 2)[1])  # 28
print(calendar.monthrange(2000, 2)[1])  # 29——400 规则胜出
print(calendar.monthrange(1900, 2)[1])  # 28——100 规则生效
print(calendar.monthrange(2024, 1)[1], calendar.monthrange(2024, 4)[1])  # 31 30
输出
29
28
29
28
31 30

Rust v1.99.1

v1.99.1
// 标准库无历法计算:公历规则四行——4 规则、100 规则、400 规则
// (C++20 chrono 的类型化版本在 GCC libstdc++ 提供)
fn days_in(y: i32, m: u32) -> u32 {
    match m {
        1 | 3 | 5 | 7 | 8 | 10 | 12 => 31,
        4 | 6 | 9 | 11 => 30,
        2 => {
            if (y % 4 == 0 && y % 100 != 0) || y % 400 == 0 {
                29
            } else {
                28
            }
        }
        _ => 0,
    }
}

fn main() {
    println!("{}", days_in(2024, 2)); // 29——闰年
    println!("{}", days_in(2023, 2)); // 28
    println!("{}", days_in(2000, 2)); // 29——400 规则胜出
    println!("{}", days_in(1900, 2)); // 28——100 规则生效
    println!("{} {}", days_in(2024, 1), days_in(2024, 4)); // 31 30
}
输出
29
28
29
28
31 30

TypeScript v26.13.1

v26.13.1
// m+1 月的第 0 天就是 m 月最后一天(此处参数 m 按人类 1-12 习惯)
const daysIn = (y: number, m: number): number => new Date(y, m, 0).getDate();
console.log(daysIn(2024, 2)); // 29——闰年
console.log(daysIn(2023, 2)); // 28
console.log(daysIn(2000, 2)); // 29——400 规则胜出
console.log(daysIn(1900, 2)); // 28——100 规则生效
console.log(daysIn(2024, 1), daysIn(2024, 4)); // 31 30
输出
29
28
29
28
31 30

语言差异说明

闰年教学四连断言:2024-02=29(4 规则)、2023-02=28(平年)、2000-02=29(400 规则胜过 100 规则)、1900-02=28(100 规则生效)——四个样本把公历历法规则完整讲清,外加 1 月 31 / 4 月 30 的大小月对照;本条为 calendar 扩展条目(扩展已确认可用,extension_loaded("calendar") 为 true,见 extension_loaded 条目)。

PHP:cal_days_in_month(CAL_GREGORIAN, $month, $year)——calendar 扩展还带 CAL_JULIAN 等历法常量与 easter_date/easter_days 复活节算法;月/年越界抛 ValueError;与 checkdate(既有条目)联动:先 cal_days_in_month 知天数,再校验具体日。

Python:calendar.monthrange(y, m) 返回 (首日星期, 天数) 元组,取 [1] 即天数——最贴对应物;calendar.isleap(y) 单独判闰;注意 Python 的 month 是 1-12(与 PHP 一致),但 calendar 模块的常量 MONDAY..SUNDAY 是 0-6。

Go:无现成函数——time.Date(y, m+1, 0, ...) 的「第 0 天归一化」惯用法即「下月第 0 天 = 本月最后一天」;时间组件全部显式传参(无隐式当前时间),确定性天然成立。

Java:YearMonth.of(y, m).lengthOfMonth() 是 1:1 对应物;LocalDate.lengthOfMonth()(需要先有日期)或 YearMonth.atEndOfMonth() 是变体;ISO 历法外可用 JapaneseChronology 等 chrono。

C#:DateTime.DaysInMonth(y, m) 是 1:1 静态方法;仅公历(GregorianCalendar)。

JavaScript/TypeScript:惯用法 new Date(y, m, 0).getDate()——JS 的 month 构造参数是 0-11(与 PHP/Python 的 1-12 差一!),传 m 本身即「下月」,日 0 回退到月末;这也是 JS 日期最著名的坑之一。

C/C++:libc 无历法 API——手写三分支规则即全部;C++20 std::chrono 有类型化答案 year_month_day_last{year(y)/month(m)/last}.day(),但本工具链的 clang libc++ 未启用该历法组(GCC libstdc++ 可用),示例走手写。

Rust:std 无历法计算——time/chrono crate 有对应 API(chrono 的 NaiveDate::from_ymd_opt(y, m, 1).days_in_month());示例手写规则,与 C/C++ 同构。