Month difference between 2 dates
Asked Answered
C

4

3

I am trying to calculate MONTH difference between one date column and today. Do we have any method in csharp like monthdiff or datediff to achieve this functionlaity? Issue with my code is if submission date year is different then it breaks.

bool isDateAccepted = ((SubmissionDate.Month - DateTime.Now.Month) < 6)
Coralline answered 7/1, 2015 at 17:55 Comment(0)
U
3

You could always add 6 months to the submission date, and compare it to the current date.

bool isDateAccepted = (submissionDate.AddMonths(6) > DateTime.Now);
Underprop answered 7/1, 2015 at 19:10 Comment(1)
Very elegant solution if the OP wants to take day of the month in to account.Ironbound
S
3

Don't directly compare the Month variables, as it will break when the month number "wraps" as you have noticed.

Instead, subtract the DateTime objects to get a TimeSpan then use that TotalDays property:

bool isDateAccepted = ((SubmissionDate - DateTime.Now).TotalDays < 6 * 30)

TimeSpan doesn't consider Months, so you'll have to define an average number of days in order to check the number for months passed.

Shoeblack answered 7/1, 2015 at 17:57 Comment(4)
Would the downvoter care to comment? I believe this is a valid approach.Shoeblack
I did because it's flawed. Take 1-Mar-14, your method would tell the system dates from 1-Mar-14 through 28-Aug-14 are in the range. But one would expect that 29 to 31-Aug-2014 are within 6 months too. You could use 365/2f but that would also have similar errors.Ironbound
@Ironbound Fair enough, I agree that using the average is not an "ideal" solution (of course, that's mostly because our month system is just as flawed).Shoeblack
@Ironbound btw, really liked your solution; thought it was really clever.Shoeblack
I
3

You could calculate the total months and subtract them:

public int MonthDifference(Date a, Date b)
{
   int totalMonthsA = a.Year*12 + a.Month;
   int totalMonthsB = b.Year*12 + b.Month;
   return totalMonthsA - totalMonthsB;
}
Ironbound answered 7/1, 2015 at 17:59 Comment(0)
U
3

You could always add 6 months to the submission date, and compare it to the current date.

bool isDateAccepted = (submissionDate.AddMonths(6) > DateTime.Now);
Underprop answered 7/1, 2015 at 19:10 Comment(1)
Very elegant solution if the OP wants to take day of the month in to account.Ironbound
N
1

It's a too late answer and as I checked no one answered about the exact fractions of the months like what we have in the TimeSpan.TotalDays for example.

for example what is the result of total months between 20-01-2021 and 20-03-2021? I guess it should be 2.0xx, but if you considered Feb as 30 days, you'll get a different answer "1.8xx or something"

So I've done the calculation according to the days in each month between the selected dates.

  • First I've got the list of days between the selected two dates as in @mqp answer
  • then I grouped them by month and did the calculation

the code:

public static double GetTotalMonths(this DateTimeOffset firstDate, DateTimeOffset thru)
{
    var days = firstDate.EachDayTill(thru).ToList();
    return days.GroupBy(d => new {d.Month, d.Year,})
        .Sum(g => (g.Count() * 1.00) / (DateTime.DaysInMonth(g.Key.Year, g.Key.Month) * 1.00));
}


public static IEnumerable<DateTimeOffset> EachDayTill(this DateTimeOffset from, DateTimeOffset thru)
{
    for (var day = new DateTimeOffset(from.Date, TimeSpan.Zero); day.Date <= thru.Date; day = day.AddDays(1))
        yield return day;
}
Needlecraft answered 14/12, 2020 at 6:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.