Программа для проверки, действительна дата или нет
Учитывая дату, проверьте, действительна она или нет. Можно предположить, что данная дата находится в диапазоне с 01.01.1800 по 31.12.9999.
Примеры :
Ввод: d = 10, m = 12, y = 2000. Выход: Да Приведенная дата 12.10.2000 действительна Ввод: d = 30, m = 2, y = 2000. Выход: Нет Указанная дата 30.02.2000 недействительна. В В феврале месяце не может быть 30 как день.
Идея проста. Нам нужно справиться со следующими вещами.
1) y, m и d находятся в допустимом диапазоне.
2) Дни в феврале находятся в допустимом диапазоне, и учитывается високосный год.
3) Обрабатываются дни в 30-дневных месяцах.
Below is the implementation to check if a given year is valid or not.
C++
// C++ program to check if // given date is valid or not. #include<iostream> using namespace std; const int MAX_VALID_YR = 9999; const int MIN_VALID_YR = 1800; // Returns true if // given year is valid. bool isLeap( int year) { // Return true if year // is a multiple pf 4 and // not multiple of 100. // OR year is multiple of 400. return (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)); } // Returns true if given // year is valid or not. bool isValidDate( int d, int m, int y) { // If year, month and day // are not in given range 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 ; // Handle February month // with leap year if (m == 2) { if (isLeap(y)) return (d <= 29); else return (d <= 28); } // Months of April, June, // Sept and Nov must have // number of days less than // or equal to 30. if (m == 4 || m == 6 || m == 9 || m == 11) return (d <= 30); return true ; } // Driver code int main( void ) { isValidDate(10, 12, 2000)? cout << "Yes
" : cout << "No
" ; isValidDate(31, 11, 2000)? cout << "Yes
" : cout << "No
" ; } |
Java
// Java program to check if // given date is valid or not. import java.io.*; class GFG { static int MAX_VALID_YR = 9999 ; static int MIN_VALID_YR = 1800 ; // Returns true if // given year is valid. static boolean isLeap( int year) { // Return true if year is // a multiple of 4 and not // multiple of 100. // OR year is multiple of 400. return (((year % 4 == 0 ) && (year % 100 != 0 )) || (year % 400 == 0 )); } // Returns true if given // year is valid or not. static boolean isValidDate( int d, int m, int y) { // If year, month and day // are not in given range 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 ; // Handle February month // with leap year if (m == 2 ) { if (isLeap(y)) return (d <= 29 ); else return (d <= 28 ); } // Months of April, June, // Sept and Nov must have // number of days less than // or equal to 30. if (m == 4 || m == 6 || m == 9 || m == 11 ) return (d <= 30 ); return true ; } // Driver code 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" ); } } // This code is contributed // by Nikita Tiwari. |
Python
# Python Program to check # if a date is valid or not 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 ) # This code is contributed by ajay0007 |
C#
// C# program to check if // given date is valid or not. using System; class GFG { const int MAX_VALID_YR = 9999; const int MIN_VALID_YR = 1800; // Returns true if // given year is valid. static bool isLeap( int year) { // Return true if year is a // multiple of 4 and not // multiple of 100. OR year // is multiple of 400. return (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)); } // Returns true if given // year is valid or not. static bool isValidDate( int d, int m, int y) { // If year, month and day // are not in given range 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 ; // Handle February month // with leap year if (m == 2) { if (isLeap(y)) return (d <= 29); else return (d <= 28); } // Months of April, June, // Sept and Nov must have // number of days less than // or equal to 30. if (m == 4 || m == 6 || m == 9 || m == 11) return (d <= 30); return true ; } // Driver code 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" ); } } // This code is contributed // by Anant Agarwal. |
PHP
<?php // PHP program to check if // given date is valid or not. // Returns true if // given year is valid. function isLeap( $year ) { // Return true if year is // a multiple of 4 and // not multiple of 100. // OR year is multiple of 400. return ((( $year % 4 == 0) && ( $year % 100 != 0)) || ( $year % 400 == 0)); } // Returns true if given // year is valid or not. function isValidDate( $d , $m , $y ) { $MAX_VALID_YR = 9999; $MIN_VALID_YR = 1800; // If year, month and day // are not in given range 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; // Handle February month // with leap year if ( $m == 2) { if (isLeap( $y )) return ( $d <= 29); else return ( $d <= 28); } // Months of April, June, // Sept and Nov must have // number of days less than // or equal to 30. if ( $m == 4 || $m == 6 || $m == 9 || $m == 11) return ( $d <= 30); return true; } // Driver code { if (isValidDate(10, 12, 2000)) echo "Yes
" ; else echo "No
" ; if (isValidDate(31, 11, 2000)) echo "Yes
" ; else echo "No
" ; } // This code is contributed // by nitin mittal. ?> |
Javascript
<script> // Javascript program to check if // given date is valid or not. const MAX_VALID_YR = 9999; const MIN_VALID_YR = 1800; // Returns true if // given year is valid. function isLeap(year) { // Return true if year // is a multiple pf 4 and // not multiple of 100. // OR year is multiple of 400. return (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)); } // Returns true if given // year is valid or not. function isValidDate(d, m, y) { // If year, month and day // are not in given range 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 ; // Handle February month // with leap year if (m == 2) { if (isLeap(y)) return (d <= 29); else return (d <= 28); } // Months of April, June, // Sept and Nov must have // number of days less than // or equal to 30. if (m == 4 || m == 6 || m == 9 || m == 11) return (d <= 30); return true ; } // Driver code isValidDate(10, 12, 2000) ? document.write( "Yes<br>" ) : document.write( "No<br>" ); isValidDate(31, 11, 2000) ? document.write( "Yes<br>" ) : document.write( "No<br>" ); // This code is contributed by souravmahato348 </script> |
Выход :
Yes No
Эта статья предоставлена RAHUL NITKKR . Если вам нравится GeeksforGeeks, и вы хотели бы внести свой вклад, вы также можете написать статью на сайте deposit.geeksforgeeks.org или отправить свою статью по электронной почте: grant@geeksforgeeks.org. Посмотрите, как ваша статья появляется на главной странице GeeksforGeeks, и помогите другим гикам.
Пожалуйста, напишите комментарии, если вы обнаружите что-то неправильное, или вы хотите поделиться дополнительной информацией по теме, обсужденной выше.
Вниманию читателя! Не прекращайте учиться сейчас. Освойте все в