Performing date comparisons in VB6
Performing date comparisons in VB6: "Performing date comparisons in VB6
by Peter Aitken | More from Peter Aitken | Published: 8/1/05
The representation of dates in VB6 makes it really easy to compare dates, yet some programmers still go to unnecessary lengths to perform a comparison. Peter Aitken demonstrates that it's a breeze to perform date comparisons in VB6.
One of the ways that VB6 really shines is in how it handles dates using the Date data type. You can display a date in any one of several standard date formats, but internally, it's represented as a serial number specifying the number of days since December 31, 1899, with negative values used for dates prior to then. You can also use the decimal portion of the value to represent a time of day, but that's beyond the scope of this tip.
The representation of dates makes it really easy to compare dates, yet some VB programmers still go to unnecessary complexity to perform a comparison. There's no need to extract the year, month, and day individually for comparison-you can compare dates directly using the standard comparison operators.
For example, the date June 30, 2004, is represented by the serial number 38168. If you want to compare it with another date, you can just compare the serial numbers to see if one date is smaller, the same, or larger than the other.
Here's an example that executes one block of statements if the date stored in the type Date variable MyDate is earlier than January 1, 2000, and another block if it's the same or later than that date.
If MyDate < #1/1/2000# Then
' Statements here are executed if MyDate is earlier.
Else
' Statements here are executed if MyDate is the same or later.
End If
This is just one example of the flexibility of VB's Date data type."
by Peter Aitken | More from Peter Aitken | Published: 8/1/05
The representation of dates in VB6 makes it really easy to compare dates, yet some programmers still go to unnecessary lengths to perform a comparison. Peter Aitken demonstrates that it's a breeze to perform date comparisons in VB6.
One of the ways that VB6 really shines is in how it handles dates using the Date data type. You can display a date in any one of several standard date formats, but internally, it's represented as a serial number specifying the number of days since December 31, 1899, with negative values used for dates prior to then. You can also use the decimal portion of the value to represent a time of day, but that's beyond the scope of this tip.
The representation of dates makes it really easy to compare dates, yet some VB programmers still go to unnecessary complexity to perform a comparison. There's no need to extract the year, month, and day individually for comparison-you can compare dates directly using the standard comparison operators.
For example, the date June 30, 2004, is represented by the serial number 38168. If you want to compare it with another date, you can just compare the serial numbers to see if one date is smaller, the same, or larger than the other.
Here's an example that executes one block of statements if the date stored in the type Date variable MyDate is earlier than January 1, 2000, and another block if it's the same or later than that date.
If MyDate < #1/1/2000# Then
' Statements here are executed if MyDate is earlier.
Else
' Statements here are executed if MyDate is the same or later.
End If
This is just one example of the flexibility of VB's Date data type."
0 Comments:
Post a Comment
<< Home