Comparing dates

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'd like to compare two dates... a string as "0306" to the date of the system clock in vb.net. If I have a string "0306", this is the format month and year. I'd like to compare it to the month and year of the system clock and see which is greater. Thanks.
 
Bryan,

Something like this should do it:

<code (VB.NET) >

Dim adate As Date = Now
If CInt(Microsoft.VisualBasic.Right(strDate, 2)) + 2000 < adate.Year
OrElse CInt(Microsoft.VisualBasic.Left(strDate, 2)) < adate.Month Then
Console.WriteLine(strDate + " is less than " + Format(adate, "MMyy"))
ElseIf CInt(Microsoft.VisualBasic.Right(strDate, 2)) + 2000 = adate.Year
And CInt(Microsoft.VisualBasic.Left(strDate, 2)) = adate.Month Then
Console.WriteLine(strDate + " is equal to " + Format(adate, "MMyy"))
Else
Console.WriteLine(strDate + " is greater than " + Format(adate, "MMyy"))
End If

</code>

-Sam Matzen


Bryan said:
I'd like to compare two dates... a string as "0306" to the date of the
system clock in vb.net. If I have a string "0306", this is the format month
and year. I'd like to compare it to the month and year of the system clock
and see which is greater. Thanks.
 
Thanks Samuel... most helpful.

Samuel L Matzen said:
Bryan,

Something like this should do it:

<code (VB.NET) >

Dim adate As Date = Now
If CInt(Microsoft.VisualBasic.Right(strDate, 2)) + 2000 < adate.Year
OrElse CInt(Microsoft.VisualBasic.Left(strDate, 2)) < adate.Month Then
Console.WriteLine(strDate + " is less than " + Format(adate, "MMyy"))
ElseIf CInt(Microsoft.VisualBasic.Right(strDate, 2)) + 2000 = adate.Year
And CInt(Microsoft.VisualBasic.Left(strDate, 2)) = adate.Month Then
Console.WriteLine(strDate + " is equal to " + Format(adate, "MMyy"))
Else
Console.WriteLine(strDate + " is greater than " + Format(adate, "MMyy"))
End If

</code>

-Sam Matzen



system clock in vb.net. If I have a string "0306", this is the format month
and year. I'd like to compare it to the month and year of the system clock
and see which is greater. Thanks.
 
Bryan,
In addition to Samual's suggestion, if I had a string & needed to compare
it to a date, I would either convert the Date to a string or convert the
string to a Date. Because your string has less detail, its missing the day,
I would convert the date to the format of the string.

Something like:

If "0306" > DateTime.Today.ToString("MMyy") Then
' "0306" is greater
End If

For information on formatting types (such as using the ToString method) see
"Formatting Types"

http://msdn.microsoft.com/library/d...y/en-us/cpguide/html/cpconformattingtypes.asp

For information on custom DateTime formats see:

http://msdn.microsoft.com/library/d...ide/html/cpconcustomdatetimeformatstrings.asp

Hope this helps
Jay

Bryan said:
I'd like to compare two dates... a string as "0306" to the date of the
system clock in vb.net. If I have a string "0306", this is the format month
and year. I'd like to compare it to the month and year of the system clock
and see which is greater. Thanks.
 
Back
Top