checkdate
checkdate(int $month, int $day, int $year): bool
Validates a Gregorian date.
C 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; // leap-year rule
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
#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; // leap-year rule
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
bool Checkdate(int year, int month, int day)
{
try
{
new DateTime(year, month, day); // out-of-range dates throw
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
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
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
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
var_dump(checkdate(2, 29, 2024));
var_dump(checkdate(2, 29, 2023));
bool(true) bool(false)
Python 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
fn main() {
// chrono: from_ymd_opt returns None for an invalid date
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
function checkdate(year: number, month: number, day: number): boolean {
const d = new Date(year, month - 1, day); // JS months are 0-based; rollovers reveal invalid dates
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
Language differences
C: the standard library has no calendar API; the example hand-writes the leap rule and month lengths.
C++: the standard library has no calendar API (calendar chrono types arrive in C++20); the example hand-writes the leap rule and month lengths.
C#: the "construction failure means invalid" idiom — new DateTime raises ArgumentOutOfRangeException.
Go: the "construct and read back" idiom — time.Date normalizes with carry-over, so the fields must match the inputs.
Java: the "construction failure means invalid" idiom — LocalDate.of raises DateTimeException.
JavaScript: the "construct and read back" idiom — Date rolls an overflow date into the next month (2023-02-29 becomes 2023-03-01).
PHP: checkdate validates a Gregorian date (including the leap rule: every 4 years, except centuries, unless divisible by 400); the argument order is month, day, year; no other language ships the same builtin.
Python: the "construction failure means invalid" idiom — datetime.date raises ValueError.
Rust: chrono's from_ymd_opt returns an Option, None when invalid.
TypeScript: same as JavaScript — overflow dates roll into the next month.