Check if a Date is Valid or Not

Last Updated : 3 Jul, 2026

Given three integers d, m, and y representing a date in the format dd/mm/yyyy, determine whether the date is valid or not. A valid date must lie between 01/01/1800 and 31/12/9999 (inclusive) and satisfy the correct number of days for the given month and year.

Examples:

Input: d = 23, m = 8, y = 2003
Output: true
Explanation: The given date 23/8/2003 is valid.

Input: d = 30, m = 2, y = 1700
Output: false
Explanation: The given date 30/2/1700 is invalid.

Try It Yourself
redirect icon

Using Direct Validation - O(1) Time and O(1) Space

The idea is to validate the given date by checking each component in sequence. First, verify that the year, month, and day lie within their valid ranges. Then, handle February separately by checking whether the year is a leap year to determine its maximum valid days. For the months having 30 days, ensure the day does not exceed 30. If all these conditions are satisfied, the date is valid; otherwise, it is invalid.

Let us understand with an example:
Input: d = 23, m = 8, y = 2003

  • Check whether the year (2003), month (8), and day (23) are within their valid ranges.
  • Since the month is August, it is neither February nor a 30-day month.
  • August has 31 days, and the given day (23) is valid.
  • All validation checks pass, so the final output is true.
C++
#include <bits/stdc++.h>
using namespace std;

int main()
{
    int d = 23, m = 8, y = 2003;

    bool ans = true;

    // Check if the year is within the valid range
    if (y < 1800 || y > 9999)
        ans = false;

    // Check if the month is valid
    else if (m < 1 || m > 12)
        ans = false;

    // Check if the day is within the basic valid range
    else if (d < 1 || d > 31)
        ans = false;

    // Handle February separately
    else if (m == 2)
    {

        // Check whether the year is a leap year
        bool leap = (y % 400 == 0) || (y % 4 == 0 && y % 100 != 0);

        // Maximum 29 days in a leap year, otherwise 28
        if ((leap && d > 29) || (!leap && d > 28))
            ans = false;
    }

    // Months having only 30 days
    else if (m == 4 || m == 6 || m == 9 || m == 11)
    {
        if (d > 30)
            ans = false;
    }

    // Print the result
    cout << (ans ? "true" : "false");

    return 0;
}
C
#include <stdbool.h>
#include <stdio.h>

int main()
{
    int d = 23, m = 8, y = 2003;

    bool ans = true;

    // Check year, month and day range
    if (y > 9999 || y < 1800)
        ans = false;
    else if (m < 1 || m > 12)
        ans = false;
    else if (d < 1 || d > 31)
        ans = false;
    else if (m == 2)
    {

        // Check leap year
        bool leap = ((y % 4 == 0 && y % 100 != 0) || (y % 400 == 0));

        if (leap)
        {
            if (d > 29)
                ans = false;
        }
        else
        {
            if (d > 28)
                ans = false;
        }
    }
    else if (m == 4 || m == 6 || m == 9 || m == 11)
    {
        if (d > 30)
            ans = false;
    }

    printf("%s\n", ans ? "true" : "false");

    return 0;
}
Java
class GFG {
    public static void main(String[] args) {

        int d = 23;
        int m = 8;
        int y = 2003;

        boolean ans = true;

        // Check year, month and day range
        if (y > 9999 || y < 1800)
            ans = false;
        else if (m < 1 || m > 12)
            ans = false;
        else if (d < 1 || d > 31)
            ans = false;
        else if (m == 2) {

            // Check leap year
            boolean leap = ((y % 4 == 0 && y % 100 != 0) || (y % 400 == 0));

            if (leap) {
                if (d > 29) ans = false;
            } else {
                if (d > 28) ans = false;
            }
        } else if (m == 4 || m == 6 || m == 9 || m == 11) {
            if (d > 30) ans = false;
        }

        System.out.println(ans);

        sc.close();
    }
}
Python
d = 23
m = 8
y = 2003

ans = True

# Check year, month and day range
if y > 9999 or y < 1800:
    ans = False
elif m < 1 or m > 12:
    ans = False
elif d < 1 or d > 31:
    ans = False
elif m == 2:

    # Check leap year
    leap = ((y % 4 == 0 and y % 100 != 0) or (y % 400 == 0))

    if leap:
        if d > 29:
            ans = False
    else:
        if d > 28:
            ans = False

elif m == 4 or m == 6 or m == 9 or m == 11:
    if d > 30:
        ans = False

if ans:
    print("true")
else:
    print("false")
C#
using System;

class GFG {
    static void Main() {
        int d = 23;
        int m = 8;
        int y = 2003;

        bool ans = true;

        // Check year, month and day range
        if (y > 9999 || y < 1800)
            ans = false;
        else if (m < 1 || m > 12)
            ans = false;
        else if (d < 1 || d > 31)
            ans = false;
        else if (m == 2) {

            // Check leap year
            bool leap = ((y % 4 == 0 && y % 100 != 0) || (y % 400 == 0));

            if (leap) {
                if (d > 29) ans = false;
            } else {
                if (d > 28) ans = false;
            }
        } else if (m == 4 || m == 6 || m == 9 || m == 11) {
            if (d > 30) ans = false;
        }

        Console.WriteLine(ans ? "true" : "false");
    }
}
JavaScript
let input = require("fs").readFileSync(0, "utf8").trim().split(/\s+/);

let index = 0;

let d = 23;
let m = 8;
let y = 2003;

let ans = true;

// Check year, month and day range
if (y > 9999 || y < 1800)
    ans = false;
else if (m < 1 || m > 12)
    ans = false;
else if (d < 1 || d > 31)
    ans = false;
else if (m == 2) {

    // Check leap year
    let leap = ((y % 4 == 0 && y % 100 != 0) || (y % 400 == 0));

    if (leap) {
        if (d > 29) ans = false;
    } else {
        if (d > 28) ans = false;
    }
} else if (m == 4 || m == 6 || m == 9 || m == 11) {
    if (d > 30) ans = false;
}

console.log(ans ? "true" : "false");

Output
true
Comment