Answer by Bhavesh Patel for Difference in months between two dates
int nMonths = 0;if (FDate.ToDateTime().Year == TDate.ToDateTime().Year) nMonths = TDate.ToDateTime().Month - FDate.ToDateTime().Month; elsenMonths = (12 - FDate.Month) + TDate.Month;
View ArticleAnswer by Konstantin Chernov for Difference in months between two dates
Here's how we approach this:public static int MonthDiff(DateTime date1, DateTime date2){ if (date1.Month < date2.Month) { return (date2.Year - date1.Year) * 12 + date2.Month - date1.Month; } else {...
View ArticleAnswer by jenson-button-event for Difference in months between two dates
I just needed something simple to cater for e.g. employment dates where only the month/year is entered, so wanted distinct years and months worked in. This is what I use, here for usefullness...
View ArticleAnswer by user687474 for Difference in months between two dates
You can use the DateDiff class of the Time Period Library for .NET:// ----------------------------------------------------------------------public void DateDiffSample(){ DateTime date1 = new DateTime(...
View ArticleAnswer by Chirag for Difference in months between two dates
To get difference in months (both start and end inclusive), irrespective of dates:DateTime start = new DateTime(2013, 1, 1);DateTime end = new DateTime(2014, 2, 1);var diffMonths = (end.Month +...
View ArticleAnswer by Paul for Difference in months between two dates
var dt1 = (DateTime.Now.Year * 12) + DateTime.Now.Month; var dt2 = (DateTime.Now.AddMonths(-13).Year * 12) + DateTime.Now.AddMonths(-13).Month; Console.WriteLine(dt1); Console.WriteLine(dt2);...
View ArticleAnswer by GreatNate for Difference in months between two dates
There are not a lot of clear answers on this because you are always assuming things.This solution calculates between two dates the months between assuming you want to save the day of month for...
View ArticleAnswer by Ivan for Difference in months between two dates
Expanded Kirks struct with ToString(format) and Duration(long ms) public struct DateTimeSpan{ private readonly int years; private readonly int months; private readonly int days; private readonly int...
View ArticleAnswer by Patrice Calvé for Difference in months between two dates
There's 3 cases: same year, previous year and other years.If the day of the month does not matter...public int GetTotalNumberOfMonths(DateTime start, DateTime end){ // work with dates in the right...
View ArticleAnswer by reza akhlaghi for Difference in months between two dates
I wrote a function to accomplish this, because the others ways weren't working for me.public string getEndDate (DateTime startDate,decimal monthCount){ int y = startDate.Year; int m = startDate.Month;...
View ArticleAnswer by Elmer for Difference in months between two dates
This worked for what I needed it for. The day of month didn't matter in my case because it always happens to be the last day of the month.public static int MonthDiff(DateTime d1, DateTime d2){ int...
View ArticleAnswer by Sukanta for Difference in months between two dates
public static int PayableMonthsInDuration(DateTime StartDate, DateTime EndDate){ int sy = StartDate.Year; int sm = StartDate.Month; int count = 0; do { count++;if ((sy == EndDate.Year) && (sm...
View ArticleAnswer by Firnas for Difference in months between two dates
You can have a function something like this.For Example, from 2012/12/27 to 2012/12/29 becomes 3 days. Likewise, from 2012/12/15 to 2013/01/15 becomes 2 months, because up to 2013/01/14 it's 1 month....
View ArticleAnswer by Tom for Difference in months between two dates
To be able to calculate the difference between 2 dates in months is a perfectly logical thing to do, and is needed in many business applications. The several coders here who have provided comments such...
View ArticleAnswer by Wayne for Difference in months between two dates
This is from my own library, will return the difference of months between two dates.public static int MonthDiff(DateTime d1, DateTime d2){ int retVal = 0; // Calculate the number of years represented...
View ArticleAnswer by Guillaume86 for Difference in months between two dates
If you want the exact number of full months, always positive (2000-01-15, 2000-02-14 returns 0), considering a full month is when you reach the same day the next month (something like the age...
View ArticleAnswer by Kirk Woll for Difference in months between two dates
Here is a comprehensive solution to return a DateTimeSpan, similar to a TimeSpan, except that it includes all the date components in addition to the time components.Usage:void Main(){ DateTime...
View ArticleAnswer by Mongus Pong for Difference in months between two dates
You could do if ( date1.AddMonths(x) > date2 )
View ArticleAnswer by Mohammad Ali for Difference in months between two dates
Public Class ClassDateOperation Private prop_DifferenceInDay As Integer Private prop_DifferenceInMonth As Integer Private prop_DifferenceInYear As Integer Public Function DayMonthYearFromTwoDate(ByVal...
View ArticleAnswer by Cheng Chen for Difference in months between two dates
I checked the usage of this method in VB.NET via MSDN and it seems that it has a lot of usages. There is no such a built-in method in C#. (Even it's not a good idea) you can call VB's in C#.Add...
View Article