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 280505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T05:07:27+00:00 2026-05-12T05:07:27+00:00

How to get difference between two dates in Year/Month/Week/Day in an efficient way? eg.

  • 0

How to get difference between two dates in Year/Month/Week/Day in an efficient way?

eg. difference between two dates is 1 Year, 2 Months, 3 Weeks, 4 Days.

Difference represents count of year(s), month(s), week(s) and day(s) between two dates.

  • 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-12T05:07:27+00:00Added an answer on May 12, 2026 at 5:07 am

    This is actually quite tricky. A different total number of days can result in the same result. For example:

    • 19th June 2008 to 19th June 2010 = 2 years, but also 365 * 2 days

    • 19th June 2006 to 19th June 2008 = 2 years, but also 365 + 366 days due to leap years

    You may well want to subtract years until you get to the point where you’ve got two dates which are less than a year apart. Then subtract months until you get to the point where you’ve got two dates which are less than a month apart.

    Further confusion: subtracting (or adding) months is tricky when you might start with a date of “30th March” – what’s a month earlier than that?

    Even further confusion (may not be relevant): even a day isn’t always 24 hours. Daylight saving anyone?

    Even further confusion (almost certainly not relevant): even a minute isn’t always 60 seconds. Leap seconds are highly confusing…

    I don’t have the time to work out the exact right way of doing this right now – this answer is mostly to raise the fact that it’s not nearly as simple as it might sound.

    EDIT: Unfortunately I’m not going to have enough time to answer this fully. I would suggest you start off by defining a struct representing a Period:

    public struct Period
    {
        private readonly int days;
        public int Days { get { return days; } }
        private readonly int months;
        public int Months { get { return months; } }
        private readonly int years;
        public int Years { get { return years; } }
    
        public Period(int years, int months, int days)
        {
            this.years = years;
            this.months = months;
            this.days = days;
        }
    
        public Period WithDays(int newDays)
        {
            return new Period(years, months, newDays);
        }
    
        public Period WithMonths(int newMonths)
        {
            return new Period(years, newMonths, days);
        }
    
        public Period WithYears(int newYears)
        {
            return new Period(newYears, months, days);
        }
    
        public static DateTime operator +(DateTime date, Period period)
        {
            // TODO: Implement this!
        }
    
        public static Period Difference(DateTime first, DateTime second)
        {
            // TODO: Implement this!
        }
    }
    

    I suggest you implement the + operator first, which should inform the Difference method – you should make sure that first + (Period.Difference(first, second)) == second for all first/second values.

    Start with writing a whole slew of unit tests – initially “easy” cases, then move on to tricky ones involving leap years. I know the normal approach is to write one test at a time, but I’d personally brainstorm a bunch of them before you start any implementation work.

    Allow yourself a day to implement this properly. It’s tricky stuff.

    Note that I’ve omitted weeks here – that value at least is easy, because it’s always 7 days. So given a (positive) period, you’d have:

    int years = period.Years;
    int months = period.Months;
    int weeks = period.Days / 7;
    int daysWithinWeek = period.Days % 7;
    

    (I suggest you avoid even thinking about negative periods – make sure everything is positive, all the time.)

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

Sidebar

Related Questions

i have created below function to get date difference between two dates.. pleas check
Which is the best way in Oracle 9i to get the difference between two
I am trying to calculate the difference between two dates. This is what I'm
I need to find the number of days between two dates : one is
I'm playing around with the jQuery Week Calendar and am trying to get this
I am trying to figure out a way to calculate the year of birth
I have two dates in format(MM/dd/yyyy hh:mm:ss:SS). For the both dates I have converted
I've used boost::gregorian::date a bit now. I can see that there are the related
Can someone explain this to me. From jconsole ... from = new Date('01/01/2010') Fri

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.