I have two dates and want difference result in months
currently i m using :
dtResult = dtExp.Subtract(dtReg);
intDays = dtResult.Days;
months = intDays / 30;
but it is giving wrong ans when there is one month difference....
can anyone help me
thx in advance
Loading

Mamta MRank #268 · 9.4K reputationPosted Mar 20, 2011, 9:44 PM EDT
I tried your code as follows (adding my own data types) and it worked successfully for all values:
DateTime dtExp, dtReg;
TimeSpan dtResult;
Console.WriteLine("Enter date ");
dtExp = Convert.ToDateTime(Console.ReadLine());
Console.WriteLine("Enter date ");
dtReg = Convert.ToDateTime(Console.ReadLine());
dtResult = dtExp.Subtract(dtReg);
int intDays = dtResult.Days;
float months = intDays / 30;
Console.WriteLine(months);
So, with what values are you getting wrong answer? Can you let me know?
Suthish NairRank #92 · 22.8K reputationPosted Mar 21, 2011, 12:12 AM EDT
int i = DateTime.Today.Subtract(DateTime.Parse("01-Mar-2011")).Days
will give difference days.
System.DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month)
will give total number of days in a month.
Now build your logic.
Mamta MRank #268 · 9.4K reputationPosted Mar 21, 2011, 1:21 AM EDT
How is your answer helpful to her? She has already used Days and already divided the days by 30 to get the month. So she is aware of the "logic" but it is not working according to her for some particular values. So instead of giving her a vague reply about the functions to use, we can instead find out which values she is testing with. I tried the code which is similar to her code, and it is working successfully, so all we need to do now is find out what was the value which gave her incorrect output.
Suthish NairRank #92 · 22.8K reputationPosted Mar 21, 2011, 1:50 AM EDT
VulpesRank #55 · 29.2K reputationPosted Mar 21, 2011, 1:55 AM EDT
Mamta MRank #268 · 9.4K reputationPosted Mar 21, 2011, 2:06 AM EDT
Suthish: I understood your point too. That's good too.
Let's wait and see her reply.
Amruta Kirdat72 reputationPosted Mar 23, 2011, 2:50 AM EDT
sry 4 late reply, due to some net prob was unable to reply.....but m back...
thx mamta,suthis,vulpes for your replies...
vulpes got exactly want i wann to say....
if there are 28 days and m dividing by 30 den it is giving 0 months which is wrong ans....
Mamta MRank #268 · 9.4K reputationPosted Mar 23, 2011, 3:07 AM EDT
eg:
if (dtExp.Month == 2)
{
months = intDays / 28;
}
else
{
months = intDays / 30;
}
But this is not complete yet because you further need to check if it is a leap year, in which case you will divide by 29. I guess you got the basic logic, now u can write the remaining code for leap year check by yourself.
Amruta Kirdat72 reputationPosted Mar 23, 2011, 3:14 AM EDT
yes i can chk leap yr n use if else.... but my basic question is dont we have any inbuilt functions to chk difference in month afterall m working on .net not vb....
VulpesRank #55 · 29.2K reputationPosted Mar 23, 2011, 3:34 AM EDT
It works by assuming that there are 31 days in each month and therefore 372 days in a whole year:
using System;
class Test
{
static void Main()
{
DateTime dt1 = new DateTime(2011, 2, 28);
DateTime dt2 = new DateTime(2011, 3, 28);
int months = DateDiffInMonths(dt1, dt2);
Console.WriteLine(months); // 1
Console.ReadKey();
}
static int DateDiffInMonths(DateTime dt1, DateTime dt2)
{
int date1 = dt1.Year * 372 + dt1.Month * 31 + dt1.Day;
int date2 = dt2.Year * 372 + dt2.Month * 31 + dt2.Day;
return (date2 - date1)/31;
}
}
If you would like to assume that 28 February 2011 to 27 March 2011 should be regarded as a whole month (which it is in some applications), then simply add 1 to date2. Notice that the routine automatically compensates for leap years.
Suthish NairRank #92 · 22.8K reputationPosted Mar 23, 2011, 6:17 AM EDT
Note: This work as same as SQL Server DateDiff method.
System.Data.Linq.SqlClient.SqlMethods.DateDiffMonth(DateTime.Parse( "01-Feb-2011"), DateTime.Parse( "02-Apr-2011")) ==> 2
System.Data.Linq.SqlClient.SqlMethods.DateDiffMonth(DateTime.Parse( "01-Feb-2011"), DateTime.Parse( "30-Mar-2011")) ==> 1
System.Data.Linq.SqlClient.SqlMethods.DateDiffMonth(DateTime.Parse( "01-Feb-2011"), DateTime.Parse( "27-Feb-2011")) ==> 0
System.Data.Linq.SqlClient.SqlMethods.DateDiffMonth(DateTime.Parse( "01-Feb-2011"), DateTime.Parse( "28-Feb-2011")) ==> 0
System.Data.Linq.SqlClient.SqlMethods.DateDiffMonth(DateTime.Parse( "01-Feb-2011"), DateTime.Parse( "01-Mar-2011")) ==> 1