469,609 Members | 1,687 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Comparing Dates?

How can compare two dates in terms of months. i.e.

01/08/2004 = 10/08/2004

05/08/2004 < 01/08/2005

thanx

Jul 21 '05 #1
3 1977
Hi Job,

You can create 2 datetime objects and compare the months of the individual
objects.

DateTime dt1=new DateTime(2004,7,10);
DateTime dt2=new DateTime(2004,8,1);
if(dt1.Month < dt2.Month)
{
MessageBox.Show("dt1 < dt2");
}

---
Bharat Biyani (bs*@orcim.com)
http://www.orcim.com

"Job Lot" wrote:
How can compare two dates in terms of months. i.e.

01/08/2004 = 10/08/2004

05/08/2004 < 01/08/2005

thanx

Jul 21 '05 #2
One option is to compare the month and year parts of the dates separately.

"Job Lot" <Jo****@discussions.microsoft.com> wrote in message
news:34**********************************@microsof t.com...
How can compare two dates in terms of months. i.e.

01/08/2004 = 10/08/2004

05/08/2004 < 01/08/2005

thanx
Jul 21 '05 #3
The approaches that have been offered work if you don't want to take the year
into account as well. In other words, if you want August 9, 2004 to equal
August 9, 2005, you can simply compare the .Month property of the DateTime
objects.

However, the following example accounts for months AND years. It is a
little bit longer, but very straightforward.

'Start with your original DateTime values
Dim date1 As DateTime = DateTime.Parse("August 5, 2004")
Dim date2 As DateTime = DateTime.Parse("February 1, 2005")

'Create "Month-Only' versions of those values
Dim month1 As New DateTime(date1.Year, date1.Month, 1)
Dim month2 As New DateTime(date2.Year, date2.Month, 1)

'Now compare
If month1 < month2 Then
MsgBox("less")
ElseIf month1 > month2 Then
MsgBox("more")
Else
MsgBox("equal")
End If
"Job Lot" wrote:
How can compare two dates in terms of months. i.e.

01/08/2004 = 10/08/2004

05/08/2004 < 01/08/2005

thanx

Jul 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Mike N. | last post: by
2 posts views Thread by Duppypog | last post: by
2 posts views Thread by Philip Townsend | last post: by
2 posts views Thread by Manny Chohan | last post: by
2 posts views Thread by ameshkin | last post: by
12 posts views Thread by colincolehour | last post: by
4 posts views Thread by cheryl | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.