0

I have two date fields where i need to caluculate difference in months between those two dates how can i do this.Below is my formula

(start.Year * 12 + start.Month) - (end.Year * 12 + end.Month);

Expected Result

Start Date      End Date      Need to get output as
08/28/2019      09/02/2019            1
06/01/2019      09/02/2019            4
01/02/2019      03/02/2019            3
01/02/2019      03/05/2019            3
Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
abc
  • 29
  • 7
  • 1
    Your title says you want to do this in C#, yet you've tagged jQuery - so which is it? In either case I'm 100% certain there is an answer to this already – Rory McCrossan Sep 11 '19 at 08:10
  • 3
    Possible duplicate of [Difference in months between two dates](https://stackoverflow.com/questions/4638993/difference-in-months-between-two-dates) – Jamiec Sep 11 '19 at 08:10
  • i had already tried them but i am not getting the expected out put – abc Sep 11 '19 at 08:11
  • from [here](https://stackoverflow.com/questions/1525990/calculating-the-difference-in-months-between-two-dates): `For example, should dates like July 5, 2009 and August 4, 2009 yield one month or zero months difference? If you say it should yield one, then what about July 31, 2009 and August 1, 2009? Is that a month? Is it simply the difference of the Month values for the dates, or is it more related to an actual span of time?`. This is very much non trivial! – Jamiec Sep 11 '19 at 08:13
  • You should tell us what output you do get. And it should be end-start, not start-end. – Andrew Morton Sep 11 '19 at 08:13
  • July 5, 2009 and August 4, 2009 1 month, July 31, 2009 and August 1, 2009? also should be one month,but 01/02/2019 03/02/2019 should yield 3 months – abc Sep 11 '19 at 08:17
  • @abc If you edit out the data that we need to help you solve the problem then the question will have to be closed. I rolled back that edit for you. – Andrew Morton Sep 12 '19 at 13:21

1 Answers1

0

Although you haven't told us what the rule is for calculating the result you're after, it looks like you need to check the day-of-month and add one if the end one is the same or later:

using System;
using System.Globalization;
using System.Linq;

namespace ConsoleApp1
{
    class Program
    {
        class DatePair
        {
            public DateTime Start { get; set; }
            public DateTime End { get; set; }

            public DatePair(string s)
            {
                var ci = new CultureInfo("en-US");
                var parts = s.Split(",".ToCharArray());
                this.Start = DateTime.Parse(parts[0], ci);
                this.End = DateTime.Parse(parts[1], ci);
            }
        }

        static void Main(string[] args)
        {
            string dats = "08/28/2019,09/02/2019;06/01/2019,09/02/2019;01/02/2019,03/02/2019;01/02/2019,03/05/2019";
            var dates = dats.Split(";".ToCharArray()).Select(p => new DatePair(p));

            foreach (DatePair d in dates)
            {
                var x = d.End.Month - d.Start.Month;
                if (d.End.Day >= d.Start.Day) { x += 1; }
                Console.WriteLine(d.Start.ToString("yyyy-MM-dd") + " " + d.End.ToString("yyyy-MM-dd") + " " + x.ToString());

            }

            Console.ReadLine();

        }
    }
}

Outputs:

2019-08-28 2019-09-02 1
2019-06-01 2019-09-02 4
2019-01-02 2019-03-02 3
2019-01-02 2019-03-05 3

I did not include the years in the calculation as there was no example date for that.

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84