checkdate

checkdate(int $month, int $day, int $year): bool

验证一个公历日期是否有效。

C C23

C23
#include <stdbool.h>
#include <stdio.h>

static bool checkdate(int year, int month, int day) {
    if (month < 1 || month > 12 || day < 1) return false;
    int leap = (year % 4 == 0 && year % 100 != 0) || year % 400 == 0; // 闰年规则
    int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    int max = days[month - 1];
    if (month == 2 && leap) max = 29;
    return day <= max;
}

int main(void) {
    printf("%s\n", checkdate(2024, 2, 29) ? "true" : "false");
    printf("%s\n", checkdate(2023, 2, 29) ? "true" : "false");
    return 0;
}
输出
true
false

C++ C++26

C++26
#include <cstdio>

static bool checkdate(int year, int month, int day) {
    if (month < 1 || month > 12 || day < 1) return false;
    int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    int max = days[month - 1];
    if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)) max = 29; // 闰年规则
    return day <= max;
}

int main() {
    std::printf("%s\n", checkdate(2024, 2, 29) ? "true" : "false");
    std::printf("%s\n", checkdate(2023, 2, 29) ? "true" : "false");
    return 0;
}
输出
true
false

C# v11.0.100

v11.0.100
bool Checkdate(int year, int month, int day)
{
    try
    {
        new DateTime(year, month, day); // 超出范围的日期抛异常
        return true;
    }
    catch (ArgumentOutOfRangeException)
    {
        return false;
    }
}
Console.WriteLine(Checkdate(2024, 2, 29) ? "true" : "false");
Console.WriteLine(Checkdate(2023, 2, 29) ? "true" : "false");
输出
true
false

Go v1.27.4

v1.27.4
package main

import (
    "fmt"
    "time"
)

func checkdate(year int, month time.Month, day int) bool {
    d := time.Date(year, month, day, 0, 0, 0, 0, time.UTC)
    return d.Year() == year && d.Month() == month && d.Day() == day
}

func main() {
    fmt.Println(checkdate(2024, time.February, 29))
    fmt.Println(checkdate(2023, time.February, 29))
}
输出
true
false

Java v27.0.1

v27.0.1
import java.time.DateTimeException;
import java.time.LocalDate;

public class Main {
    static boolean checkdate(int year, int month, int day) {
        try {
            LocalDate.of(year, month, day);
            return true;
        } catch (DateTimeException e) {
            return false;
        }
    }

    public static void main(String[] args) {
        System.out.println(checkdate(2024, 2, 29));
        System.out.println(checkdate(2023, 2, 29));
    }
}
输出
true
false

JavaScript v26.13.1

v26.13.1
function checkdate(year, month, day) {
  const d = new Date(year, month - 1, day);
  return d.getFullYear() === year && d.getMonth() === month - 1 && d.getDate() === day;
}
console.log(checkdate(2024, 2, 29));
console.log(checkdate(2023, 2, 29));
输出
true
false

PHP v8.6.0

v8.6.0
var_dump(checkdate(2, 29, 2024));
var_dump(checkdate(2, 29, 2023));
输出
bool(true)
bool(false)

Python v3.15.2

v3.15.2
import datetime

def checkdate(year, month, day):
    try:
        datetime.date(year, month, day)
        return True
    except ValueError:
        return False

print(checkdate(2024, 2, 29))
print(checkdate(2023, 2, 29))
输出
True
False

Rust v1.99.1

v1.99.1
fn main() {
    // chrono:from_ymd_opt 对非法日期返回 None
    println!("{}", chrono::NaiveDate::from_ymd_opt(2024, 2, 29).is_some());
    println!("{}", chrono::NaiveDate::from_ymd_opt(2023, 2, 29).is_some());
}
输出
true
false

TypeScript v26.13.1

v26.13.1
function checkdate(year: number, month: number, day: number): boolean {
  const d = new Date(year, month - 1, day); // JS 月份从 0 起;溢出滚动会暴露非法日期
  return d.getFullYear() === year && d.getMonth() === month - 1 && d.getDate() === day;
}
console.log(checkdate(2024, 2, 29));
console.log(checkdate(2023, 2, 29));
输出
true
false

语言差异说明

C:标准库无日历 API,示例手写闰年与每月天数。

C++:标准库无日历 API(C++20 才有 std::chrono 日历类型),示例手写闰年与每月天数。

C#:惯用法「构造失败即非法」——new DateTime 抛 ArgumentOutOfRangeException。

Go:惯用法「构造后回读比对」——time.Date 会规范化进位,回读字段与入参一致才有效。

Java:惯用法「构造失败即非法」——LocalDate.of 抛 DateTimeException。

JavaScript:惯用法「构造后回读比对」——Date 会把溢出日期滚动到下月(2023-02-29 变 2023-03-01)。

PHP:checkdate 校验公历日期(含闰年规则:4 年一闰、百年不闰、400 年再闰),参数顺序是月、日、年;各语言没有同名内置。

Python:惯用法「构造失败即非法」——datetime.date 抛 ValueError。

Rust:chrono from_ymd_opt 返回 Option,非法即 None。

TypeScript:同 JavaScript,溢出日期滚动到下月。