Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 945065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:41:37+00:00 2026-05-15T22:41:37+00:00

Example: given two dates below, finish is always greater than or equal to start

  • 0

Example: given two dates below, finish is always greater than or equal to start

start = 2001 Jan 01

finish = 2002 Mar 15

So from 2001 Jan 01 to the end of 2002 Feb

months = 12 + 2 = 14

For 2002 March

15/30 = 0.5

so grand total is 14.5 months difference.

It’s very easy to work out by hand but how do I code it elegantly? At the moment I have the combination of a lot of if else and while loops to achieve what I want but I believe there are simpler solutions out there.

Update: the output needs to be precise (not approximation) for example:
if start 2001 Jan 01 and finish 2001 Apr 16, the output should be 1 + 1 + 1= 3 (for Jan, Feb and Mar) and 16 / 31 = 0.516 month, so the total is 3.516.

Another example would be if I start on 2001 Jul 5 and finish on 2002 Jul 10, the output should be 11 month up to the end of June 2002, and (31-5)/31 = 0.839 and 10/31 = 0.323 months, so the total is 11 + 0.839 + 0.323 = 12.162.

I extended Josh Stodola‘s code and Hightechrider‘s code:

public static decimal GetMonthsInRange(this IDateRange thisDateRange)
{
    var start = thisDateRange.Start;
    var finish = thisDateRange.Finish;

    var monthsApart = Math.Abs(12*(start.Year - finish.Year) + start.Month - finish.Month) - 1;

    decimal daysInStartMonth = DateTime.DaysInMonth(start.Year, start.Month);
    decimal daysInFinishMonth = DateTime.DaysInMonth(finish.Year, finish.Month);

    var daysApartInStartMonth = (daysInStartMonth - start.Day + 1)/daysInStartMonth;
    var daysApartInFinishMonth = finish.Day/daysInFinishMonth;

    return monthsApart + daysApartInStartMonth + daysApartInFinishMonth;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T22:41:38+00:00Added an answer on May 15, 2026 at 10:41 pm

    I gave an int answer before, and then realized what you asked for a more precise answer. I was tired, so I deleted and went to bed. So much for that, I was unable to fall asleep! For some reason, this question really bugged me, and I had to solve it. So here you go…

    static void Main(string[] args)
    {
        decimal diff;
    
        diff = monthDifference(new DateTime(2001, 1, 1), new DateTime(2002, 3, 15));
        Console.WriteLine(diff.ToString("n2")); //14.45
    
        diff = monthDifference(new DateTime(2001, 1, 1), new DateTime(2001, 4, 16));
        Console.WriteLine(diff.ToString("n2")); //3.50
    
        diff = monthDifference(new DateTime(2001, 7, 5), new DateTime(2002, 7, 10));
        Console.WriteLine(diff.ToString("n2")); //12.16
    
        Console.Read();
    }
    
    static decimal monthDifference(DateTime d1, DateTime d2)
    {
        if (d1 > d2)
        {
            DateTime hold = d1;
            d1 = d2;
            d2 = hold;
        }
    
        int monthsApart = Math.Abs(12 * (d1.Year-d2.Year) + d1.Month - d2.Month) - 1;
        decimal daysInMonth1 = DateTime.DaysInMonth(d1.Year, d1.Month);
        decimal daysInMonth2 = DateTime.DaysInMonth(d2.Year, d2.Month);
    
        decimal dayPercentage = ((daysInMonth1 - d1.Day) / daysInMonth1)
                              + (d2.Day / daysInMonth2);
        return monthsApart + dayPercentage;
    }
    

    Now I shall have sweet dreams. Goodnight 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 496k
  • Answers 496k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If you want to both move the cursor and the… May 16, 2026 at 11:42 am
  • Editorial Team
    Editorial Team added an answer This is what you're looking for: Application.ScreenUpdating = False Just… May 16, 2026 at 11:42 am
  • Editorial Team
    Editorial Team added an answer This isn't the clearest question, but I'm going to assume:… May 16, 2026 at 11:42 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

For example, given two dates in input boxes: <input id="first" value="1/1/2000"/> <input id="second" value="1/1/2001"/>
Given two dates, what is the best method to calculate the number of days
Given a tabbed delimited text file with two columns name and date: Sam 20100101
Given a set of two or more logical conditions, is it possible to algorithmically
Given an integer N I want to find two integers A and B that
The code below demonstrates the problematic joda-time implementation of week calculation. This behavior is
I want to select blocks of text within given string. These blocks of text
I want to rbind.zoo two zoo object together. When I was testing I came
Summary: Given an array {a, b, ..., w, x, ..., z} insert several elements
Given the following template: template <typename T> class wrapper : public T {}; What

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.