By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,333 Members | 1,022 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,333 IT Pros & Developers. It's quick & easy.

Comparing dates

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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.