469,934 Members | 1,830 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,934 developers. It's quick & easy.

Comparing dates

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.
Nov 20 '05 #1
3 4205
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" <Br***@discussions.microsoft.com> wrote in message
news:55**********************************@microsof t.com...
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.
Nov 20 '05 #2
Thanks Samuel... most helpful.

"Samuel L Matzen" wrote:
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" <Br***@discussions.microsoft.com> wrote in message
news:55**********************************@microsof t.com...
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.

Nov 20 '05 #3
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/de...ttingtypes.asp

For information on custom DateTime formats see:

http://msdn.microsoft.com/library/de...matstrings.asp

Hope this helps
Jay

"Bryan" <Br***@discussions.microsoft.com> wrote in message
news:55**********************************@microsof t.com...
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.
Nov 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Job Lot | last post: by
2 posts views Thread by Duppypog | last post: by
2 posts views Thread by Philip Townsend | last post: by
4 posts views Thread by Working_Girl | last post: by
12 posts views Thread by colincolehour | last post: by
2 posts views Thread by cyber100 | last post: by
4 posts views Thread by cheryl | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.