How to compare 2 dates ?

Rexyss

Member
Joined
Jan 3, 2008
Messages
12
Programming Experience
Beginner
Hi,i need a way to compare todates.

Eksampel:

VB.NET:
Dim Date1 as Date
Dim Date2 as Date

Date1="01-12-2005"
Date2="15-05-2007"

If Date1 > Date2 Then
Msgbox "Date1 is the newest"
Else
Msgbox "Date2 is the newest"
Endif

This works fine if both Dates are in the same year, but not else.
How to du this a better way?
 
What is it you want to compare? Only day and month? If so you have to compare these values yourself.
 
No The Dates are in this format "08-12-2007 11:53:33"

And i need a way to se witch one there are the newest...
 
A Date is a date value not a string. The comparison you posted first works for me, it says the 2007 date is the most recent.
 
Dates aren't in any format. A Date just contains a date and time value as a number. Format is only an issue when representing date/time values as strings, which normally means when displaying to the user.

As JohnH has said, if you have Date values then the code you've already posted will work. If you have String object then to convert them to Dates you should use Date.Parse, ParseExact, TryParse or TryParseExact, depending on the circumstances. Where are these values coming from? Do they actually require conversion from String to Date, or are they Dates already?
 
Back
Top