Solved

C# DateDiff in Months

Posted on 2014-11-06
2
311 Views
Last Modified: 2014-11-12
I need to get the difference in months between two dates.

In SQL, this is what I need:
select DATEDIFF(MONTH, '11/1/2014', DATEADD(mm, DATEDIFF(mm, 0, '12/31/2014') + 1, 0)) 

Open in new window


The above date difference should be 2 months.
Difference between 12/10/2014 and 12/20/2014 = 1 month.
Difference between 11/1/2012 and 12/31/2014 = 26 months.

How do I do the same in C#?
0
Comment
Question by:pzozulka
2 Comments
 
LVL 14

Accepted Solution

by:
Tchuki earned 250 total points
ID: 40427398
The following should give what you are looking for:

namespace EE.Q_28552505.ConsoleApp
{
    using System;

    internal class Program
    {
        private static void Main(string[] args)
        {
            var dateTimeA = new DateTime(2013, 11, 1);
            var dateTimeB = DateTime.UtcNow;

            var difference = ((dateTimeB.Year - dateTimeA.Year) * 12) + dateTimeB.Month - dateTimeA.Month;

            Console.WriteLine(difference); // output is 12

            Console.ReadKey();
        }
    }
}

Open in new window

0
 
LVL 49

Assisted Solution

by:Gustav Brock
Gustav Brock earned 250 total points
ID: 40434947
Assuming there is some specifics behind the "custom" calculation, it is probably much more straight-forward to mimic it in VB.NET:
using System;
using Microsoft.VisualBasic;

namespace DateDiff
{
    class Program
    {
        static void Main(string[] args)
        {
            DateTest test = new DateTest ();

            //DateTime date1 = new DateTime(2014,11,1);
            //DateTime date2 = new DateTime(2014,12,21);
            //DateTime date1 = new DateTime(2014, 12, 10);
            //DateTime date2 = new DateTime(2014, 12, 20);
            DateTime date1 = new DateTime(2012, 11, 1);
            DateTime date2 = new DateTime(2014, 12, 21);

            long diff = test.DDiff(date1, date2);
            Console.WriteLine(diff.ToString());
            Console.Read();
        }
    }

    public class DateTest
    {
      
        public long DDiff(DateTime Date1,DateTime Date2) 
        {
            DateTime dat0 = new DateTime(1899, 12, 30);
            return DateAndTime.DateDiff("M", Date1, DateAndTime.DateAdd("M", DateAndTime.DateDiff("M", dat0, Date2) + 1, dat0));
        }

    }

}

Open in new window

This returns the expected results.

/gustav
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now