Answer by phil123456 for Difference in months between two dates
Insane method that counts all days, so super precisehelper class :public class DaysInMonth{ public int Days { get; set; } public int Month { get; set; } public int Year { get; set; } public bool Full {...
View ArticleAnswer by Gambitier for Difference in months between two dates
Apart from all given answers I find this piece of code very straightforward. AS DateTime.MinValue is 1/1/1, we have to subtract 1 from month, years and days. var timespan =...
View ArticleAnswer by Shah Zaiƞ for Difference in months between two dates
You can use Noda Time https://nodatime.org/LocalDate start = new LocalDate(2010, 1, 5);LocalDate end = new LocalDate(2012, 6, 1);Period period = Period.Between(start, end,...
View ArticleAnswer by Michael for Difference in months between two dates
This simple static function calculates the fraction of months between two Datetimes, e.g. 1.1. to 31.1. = 1.01.4. to 15.4. = 0.516.4. to 30.4. = 0.51.3. to 1.4. = 1 + 1/30 The function assumes that the...
View ArticleAnswer by Mircea Ion for Difference in months between two dates
It seems that the DateTimeSpan solution pleases a lot of people. I don't know. Let's consider the:BeginDate = 1972/2/29 EndDate = 1972/4/28.The DateTimeSpan based answer is:1 year(s), 2 month(s) and 0...
View ArticleAnswer by Eduardo Pelais for Difference in months between two dates
My problem was solved with this solution:static void Main(string[] args) { var date1 = new DateTime(2018, 12, 05); var date2 = new DateTime(2019, 03, 01); int CountNumberOfMonths() => (date2.Month -...
View ArticleAnswer by Tommix for Difference in months between two dates
Simple and fast solution to count total months between 2 dates.If you want to get only different months, not counting the one that is in From date - just remove +1 from code.public static int...
View ArticleAnswer by Dan Sutton for Difference in months between two dates
Based on the excellent DateTimeSpan work done above, I've normalized the code a bit; this seems to work pretty well:public class DateTimeSpan{ private DateTimeSpan() { } private DateTimeSpan(int years,...
View ArticleAnswer by Patrick Oniel Bernardo for Difference in months between two dates
public partial class Form1 : Form{ public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { label3.Text = new DateDifference(Convert.ToDateTime("2018-09-13"),...
View ArticleAnswer by Mohammad Shahnawaz Alam for Difference in months between two dates
Simple fix. Works 100% var exactmonth = (date1.Year - date2.Year) * 12 + date1.Month - date2.Month + (date1.Day >= date2.Day ? 0 : -1); Console.WriteLine(exactmonth);
View ArticleAnswer by Ahmed for Difference in months between two dates
In my case it is required to calculate the complete month from the start date to the day prior to this day in the next month or from start to end of month.Ex: from 1/1/2018 to 31/1/2018 is a complete...
View ArticleAnswer by John A for Difference in months between two dates
This is in response to Kirk Woll's answer. I don't have enough reputation points to reply to a comment yet... I liked Kirk's solution and was going to shamelessly rip it off and use it in my code, but...
View ArticleAnswer by Morgs for Difference in months between two dates
Here is my contribution to get difference in Months that I've found to be accurate:namespace System{ public static class DateTimeExtensions { public static Int32 DiffMonths( this DateTime start,...
View ArticleAnswer by Simon Mourier for Difference in months between two dates
Here is a simple solution that works at least for me. It's probably not the fastest though because it uses the cool DateTime's AddMonth feature in a loop:public static int GetMonthsDiff(DateTime start,...
View ArticleAnswer by Chethaka Wickramarathne for Difference in months between two dates
LINQ Solution,DateTime ToDate = DateTime.Today;DateTime FromDate = ToDate.Date.AddYears(-1).AddDays(1);int monthCount = Enumerable.Range(0, 1 + ToDate.Subtract(FromDate).Days) .Select(x =>...
View ArticleAnswer by Brent for Difference in months between two dates
Here's a much more concise solution using VB.Net DateDiff for Year, Month, Day only. You can load the DateDiff library in C# as well.date1 must be <= date2VB.NETDim date1 = Now.AddDays(-2000)Dim...
View ArticleAnswer by Saeed Mahmoudi for Difference in months between two dates
The most precise way is this that return difference in months by fraction :private double ReturnDiffereceBetweenTwoDatesInMonths(DateTime startDateTime, DateTime endDateTime){ double result = 0; double...
View ArticleAnswer by Waleed A.K. for Difference in months between two dates
you can use the following extension:Codepublic static class Ext{ #region Public Methods public static int GetAge(this DateTime @this) { var today = DateTime.Today; return ((((today.Year - @this.Year) *...
View ArticleAnswer by George Mavritsakis for Difference in months between two dates
My understanding of the total months difference between 2 dates has an integral and a fractional part (the date matters).The integral part is the full months difference.The fractional part, for me, is...
View ArticleAnswer by Edward Brey for Difference in months between two dates
Use Noda Time:LocalDate start = new LocalDate(2013, 1, 5);LocalDate end = new LocalDate(2014, 6, 1);Period period = Period.Between(start, end, PeriodUnits.Months);Console.WriteLine(period.Months); //...
View ArticleAnswer 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 ArticleAnswer by Adam Ralph for Difference in months between two dates
Assuming the day of the month is irrelevant (i.e. the diff between 2011.1.1 and 2010.12.31 is 1), with date1 > date2 giving a positive value and date2 > date1 a negative value((date1.Year -...
View ArticleDifference in months between two dates
How to calculate the difference in months between two dates in C#?Is there is equivalent of VB's DateDiff() method in C#. I need to find difference in months between two dates that are years apart. The...
View ArticleAnswer by Miguel for Difference in months between two dates
In case you just care about the month and year and want to touch both dates (for example you want to go thru JAN/2021 to AGO/2022) you can use this:int numberOfMonths= (Year2 > Year1 ? ( Year2 -...
View ArticleAnswer by Adam Janovec for Difference in months between two dates
one line solutionFor first, check if both dates are in current year, if not get months of whole years and then add months from start and end year.DateTime dateFrom = new DateTime(2019, 2, 1);DateTime...
View ArticleAnswer by Dan Hilderbrand for Difference in months between two dates
I was working on a project that only delt in years and months. Here is a solution that may help./// <summary>/// Get the total months between two date. This will count whole months and not care...
View ArticleAnswer by EgoPingvina for Difference in months between two dates
Someone must have done it))The extension method returns the number of full months between the given dates. No matter in what order the dates are received, a natural number will always be returned.No...
View Article