Program to check if a date is valid or not
Last Updated :
16 Feb, 2023
Given a date, check if it is valid or not. It may be assumed that the given date is in range from 01/01/1800 to 31/12/9999.
Examples :
Input : d = 10, m = 12, y = 2000
Output : Yes
The given date 10/12/2000 is valid
Input : d = 30, m = 2, y = 2000
Output : No
The given date 30/2/2000 is invalid. The
February month cannot have 30 as day.
The idea is simple. We need to handle following things.
1) y, m and d are in allowed range.
2) Days in February are in allowed range and leap year is handled.
3) Days in 30 day months are handled.
Below is the implementation to check if a given year is valid or not.
C++
#include<iostream>
using namespace std;
const int MAX_VALID_YR = 9999;
const int MIN_VALID_YR = 1800;
bool isLeap( int year)
{
return (((year % 4 == 0) &&
(year % 100 != 0)) ||
(year % 400 == 0));
}
bool isValidDate( int d, int m, int y)
{
if (y > MAX_VALID_YR ||
y < MIN_VALID_YR)
return false ;
if (m < 1 || m > 12)
return false ;
if (d < 1 || d > 31)
return false ;
if (m == 2)
{
if (isLeap(y))
return (d <= 29);
else
return (d <= 28);
}
if (m == 4 || m == 6 ||
m == 9 || m == 11)
return (d <= 30);
return true ;
}
int main( void )
{
isValidDate(10, 12, 2000)? cout << "Yes\n" :
cout << "No\n" ;
isValidDate(31, 11, 2000)? cout << "Yes\n" :
cout << "No\n" ;
}
|
Java
import java.io.*;
class GFG
{
static int MAX_VALID_YR = 9999 ;
static int MIN_VALID_YR = 1800 ;
static boolean isLeap( int year)
{
return (((year % 4 == 0 ) &&
(year % 100 != 0 )) ||
(year % 400 == 0 ));
}
static boolean isValidDate( int d,
int m,
int y)
{
if (y > MAX_VALID_YR ||
y < MIN_VALID_YR)
return false ;
if (m < 1 || m > 12 )
return false ;
if (d < 1 || d > 31 )
return false ;
if (m == 2 )
{
if (isLeap(y))
return (d <= 29 );
else
return (d <= 28 );
}
if (m == 4 || m == 6 ||
m == 9 || m == 11 )
return (d <= 30 );
return true ;
}
public static void main(String args[])
{
if (isValidDate( 10 , 12 , 2000 ))
System.out.println( "Yes" );
else
System.out.println( "No" );
if (isValidDate( 31 , 11 , 2000 ))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
import datetime
def date_validation(day, month, year):
isValidDate = True
try :
datetime.datetime( int (year),
int (month), int (day))
except ValueError :
isValidDate = False
if (isValidDate) :
print ( "Yes" )
else :
print ( "No" )
date_validation( 10 , 12 , 2000 )
date_validation( 31 , 11 , 2000 )
|
C#
using System;
class GFG
{
const int MAX_VALID_YR = 9999;
const int MIN_VALID_YR = 1800;
static bool isLeap( int year)
{
return (((year % 4 == 0) &&
(year % 100 != 0)) ||
(year % 400 == 0));
}
static bool isValidDate( int d,
int m,
int y)
{
if (y > MAX_VALID_YR ||
y < MIN_VALID_YR)
return false ;
if (m < 1 || m > 12)
return false ;
if (d < 1 || d > 31)
return false ;
if (m == 2)
{
if (isLeap(y))
return (d <= 29);
else
return (d <= 28);
}
if (m == 4 || m == 6 ||
m == 9 || m == 11)
return (d <= 30);
return true ;
}
public static void Main()
{
if (isValidDate(10, 12, 2000))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
if (isValidDate(31, 11, 2000))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
PHP
<?php
function isLeap( $year )
{
return ((( $year % 4 == 0) &&
( $year % 100 != 0)) ||
( $year % 400 == 0));
}
function isValidDate( $d , $m , $y )
{
$MAX_VALID_YR = 9999;
$MIN_VALID_YR = 1800;
if ( $y > $MAX_VALID_YR ||
$y < $MIN_VALID_YR )
return false;
if ( $m < 1 || $m > 12)
return false;
if ( $d < 1 || $d > 31)
return false;
if ( $m == 2)
{
if (isLeap( $y ))
return ( $d <= 29);
else
return ( $d <= 28);
}
if ( $m == 4 || $m == 6 ||
$m == 9 || $m == 11)
return ( $d <= 30);
return true;
}
{
if (isValidDate(10, 12, 2000))
echo "Yes\n" ;
else
echo "No\n" ;
if (isValidDate(31, 11, 2000))
echo "Yes\n" ;
else
echo "No\n" ;
}
?>
|
Javascript
<script>
const MAX_VALID_YR = 9999;
const MIN_VALID_YR = 1800;
function isLeap(year)
{
return (((year % 4 == 0) &&
(year % 100 != 0)) ||
(year % 400 == 0));
}
function isValidDate(d, m, y)
{
if (y > MAX_VALID_YR ||
y < MIN_VALID_YR)
return false ;
if (m < 1 || m > 12)
return false ;
if (d < 1 || d > 31)
return false ;
if (m == 2)
{
if (isLeap(y))
return (d <= 29);
else
return (d <= 28);
}
if (m == 4 || m == 6 ||
m == 9 || m == 11)
return (d <= 30);
return true ;
}
isValidDate(10, 12, 2000) ? document.write( "Yes<br>" ) :
document.write( "No<br>" );
isValidDate(31, 11, 2000) ? document.write( "Yes<br>" ) :
document.write( "No<br>" );
</script>
|
Output :
Yes
No
Time Complexity: O(1)
Auxiliary Space: O(1), since no extra space has been taken.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...