472,342 Members | 1,681 Online

# 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 2071
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.

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 thread has been closed and replies have been disabled. Please start a new discussion.

### Similar topics

 4 by: F | last post by: Hi I have posted the question few days back about problem in inserting the dates in SQL server and thankful to them who replied. That was solved... 2 by: Mike N. | last post by: I am currently using a function to validate a form on the client side (see code below). At the end of the function, I would like it to also compare... 2 by: Duppypog | last post by: I'm trying to compare a date stored in a database with today's date using an If statement, but it's not returning true. Example, value in database... 2 by: Philip Townsend | last post by: I am having difficulty with a simple routine as follows: public static bool VerifyExpirationDate(DateTime date) { if(date>=DateTime.Now)return... 2 by: Manny Chohan | last post by: Hi, i have two datetime values in format 11/22/04 9:00 AM and 11/22/04 9:30 AM. How can i compare dates .net c# or if there is any other way such as... 2 by: ameshkin | last post by: Hi GUys, Im trying to compare two dates in MYSQL. But its not treating the dates as numbers, but as strings. I try using strtotime but that did... 12 by: colincolehour | last post by: I am new to Python and am working on my first program. I am trying to compare a date I found on a website to todays date. The problem I have is the... 4 by: cheryl | last post by: I am using the PHP.MYSQL and Apache server application in developing my website. I have problem in comparing dates. Website has room reservation, the... 2 by: dantebothermy | last post by: Is there a simple way to subtract the time from a datetime field, so when I compare dates, I'm always comparing the dates at 12:00 am? I'd... 4 by: jupi13 | last post by: i have this code..i don't know what where is the error in this one..it says data type mismatch..... Dim Mydate As Date Dim MydateString As String... 0 by: better678 | last post by: Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented... 0 by: teenabhardwaj | last post by: How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of... 0 by: CD Tom | last post by: This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins... 0 by: jalbright99669 | last post by: Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made... 0 by: antdb | last post by: Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was... 0 by: Matthew3360 | last post by: Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ... 0 by: Arjunsri | last post by: I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and... 0 by: WisdomUfot | last post by: It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific... 0 by: Matthew3360 | last post by: Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...