Important alert: (current site time 7/15/2013 9:11:53 PM EDT)
 

VB icon

Check for valid Date

Email
Submitted on: 10/10/2001 3:44:47 AM
By: sp 
Level: Intermediate
User Rating: By 2 Users
Compatibility: 5.0 (all versions), 4.0 (all versions)
Views: 11165
author picture
(About the author)
 
     This subroutine simply checks if a given date is valid. It also takes leap years into account. Please rate my code.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
=**************************************
= Name: Check for valid Date
= Description:This subroutine simply checks if a given date is valid. It also takes leap years into account. Please rate my code.
= By: sp
=
= Inputs:$datein -> Required string containing a date in "dd/mm/yyyy" format
=
= Returns:1 # date okay
-1 # day invalid
-2 # month invalid
-3 # year invalid
=
=This code is copyrighted and has= limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=240&lngWId=6=for details.=**************************************

##################################################
#
# This subroutine simply checks if a date is valid
# Usage: if (&isdate($datein)) { print "ok!" ; }
# $datein -> Required - "dd/mm/yyyy" format
# Returns: 1 # date okay
#	-1 # day invalid
#	-2 # month invalid
#	-3 # year invalid
#
##################################
sub isDate {
 my ($dd,$mm,$yy) = split(/\//,@_[0]) ;
 # make sure that $yy is 4 digits
 if ($yy =~ /^\d\d$/) {
if ($yy < 70) {
 # will assume after 70 is in the 1900's
 #u can change this... 
 $yy += 2000 ;
} else {
 $yy += 1900 ;
}
 }
 @monthDays = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31) ;
 # check for leap year - Thanks Megan :-)
 if ($yy == int($yy/400)*400) {
$monthDays[1] = 29;
 } elsif ($yy != int($yy/100)*100) {
if ($yy == int($yy/4)*4) {
 $monthDays[1] = 29;
}
 }
 return -3 if ($yy !~ /^\d+$/) ;
 return -2 if (!(($mm > 0) && ($mm <= 12))) ;
 return -1 if (!(($dd > 0) && ($dd <= $monthDays[$mm-1]))) ;
 return 1 ;
}


Other 3 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments
10/11/2001 8:40:09 AMMarkus

Your leap year evaluation is invalid. Try using modulus to determine whether or not it is a leap year, i.e., $yy%4 == 0 instead.
(If this comment was disrespectful, please report it.)

 
11/11/2002 7:39:58 PM

#Leap year calculation
#1) determine if leap century (/400)
#2) determine if leap year (/4) is also
# divisible by 100. If so, NOT a
# leap year
if ($yy == int($yy/400)*400) {
$monthDays[1] = 29;
} elsif ($yy != int($yy/100)*100) {
if ($yy == int($yy/4)*4) {
$monthDays[1] = 29;
}
}
#hope the indenting held up :)
(If this comment was disrespectful, please report it.)

 
3/5/2003 10:02:07 PM

This can be done much more easily using timelocal.
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.