Important alert: (current site time 7/15/2013 8:51:54 PM EDT)
 

VB icon

Compare two dates

Email
Submitted on: 6/3/2002 3:27:17 PM
By: John Winger 
Level: Intermediate
User Rating: By 1 Users
Compatibility: 5.0 (all versions)
Views: 16844
(About the author)
 
     This routine compares two dates and returns a number indicating how the first date compares to the second (greater than, less than, or equal to).
 
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: Compare two dates
= Description:This routine compares two dates and returns a number indicating how the first date compares to the second (greater than, less than, or equal to).
= By: John Winger
=
= Inputs:It takes two arguments: two dates formatted like YYYY-MM-DD[ hh:mm:ss] (like from MySQL for instance).
=
= Returns:The routine returns -1 if date1 is less than date2, 1 if date1 is greater than date2, or 0 if they are equal.
=
= Assumes:If the two dates contain different amounts of information (like one includes the time and the other doesn't) then they are compared up to the lesser amount of info.
=
=This code is copyrighted and has= limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=324&lngWId=6=for details.=**************************************

##
## Written by John Winger (john@wingeronline.com)
##
## Use as you wish, but as a courtesy I ask that you give me credit in your code. 
##
print "This script is testing a routine to compare 2 dates.\n\n";
$dtcompare = "2002-03-04 14:36:15";
print "Enter a date to compare against $dtcompare: ";
$userdate = <STDIN>;
chomp($userdate);
$comp_results = compareDbDateTime($userdate, $dtcompare);
if($comp_results == -1) { print "\t$userdate is less than $dtcompare\n\n"}
elsif ($comp_results == 0) { print "\t$userdate is equal to $dtcompare\n\n" }
elsif ($comp_results == 1) { print "\t$userdate is greater than $dtcompare\n\n" }
else{ print "\tError\n\n"}
sub compareDbDateTime
{
	# answers how does date1 compare to date2
	# (greater than "1", less than "-1", or equal to "0")
	my ($dt1, $dt2) = @_;
	
	my @datetime1;
	my @datetime2;
	my $limit = 0;
	
	my ($date1, $time1) = split(/ /, $dt1);
	push(@datetime1, split(/-/, $date1));
	push(@datetime1, split(/:/, $time1));
	
	my ($date2, $time2) = split(/ /, $dt2);
	push(@datetime2, split(/-/, $date2));
	push(@datetime2, split(/:/, $time2));
	
	# compare up to the lesser number of elements
	# (like if one datetime only has a date and no time, don't try to compare time)
	if(@datetime1 == @datetime2) { $limit = @datetime1 }
	elsif (@datetime1 > @datetime2) { $limit = @datetime2 }
	elsif (@datetime1 < @datetime2) { $limit = @datetime1 }
	
	for (my $i = 0; $i < $limit; $i++)
	{
		if ($datetime1[$i] > $datetime2[$i]) { return 1; last; }# date1 greater than date2
		if ($datetime1[$i] < $datetime2[$i]) { return -1; last; }# date1 less than date2
	}
	return 0;# dates are equal
}


Other 11 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


 There are no comments on this submission.
 

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.