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
Suthish NairPosted Mar 23, 2011, 10:17 AM
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
VulpesPosted Mar 23, 2011, 7:34 AM
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.
Amruta KirdatPosted Mar 23, 2011, 7:14 AM
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....
Mamta MPosted Mar 23, 2011, 7:07 AM
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 KirdatPosted Mar 23, 2011, 6:50 AM
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 MPosted Mar 21, 2011, 6:06 AM
Suthish: I understood your point too. That's good too.
Let's wait and see her reply.
VulpesPosted Mar 21, 2011, 5:55 AM
Suthish NairPosted Mar 21, 2011, 5:50 AM
Mamta MPosted Mar 21, 2011, 5:21 AM
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 NairPosted Mar 21, 2011, 4:12 AM
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 MPosted Mar 21, 2011, 1:44 AM
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?