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

  • Home
  • SEARCH
  • 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 218013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:42:40+00:00 2026-05-11T18:42:40+00:00

Can anyone tell me how to calculate the diff between two dates without using

  • 0

Can anyone tell me how to calculate the diff between two dates without using .net datetime and timestamp classes?

Thanks

  • 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-11T18:42:40+00:00Added an answer on May 11, 2026 at 6:42 pm

    If I couldn’t use the built-in types, I’d try to find another .NET date/time API. I suspect I wouldn’t find one though, given that any sane person would use the built-in ones.

    If it’s really just a case of dates, I guess a full date/time API isn’t really required. Parse whatever you get into “days since some epoch” (e.g. Jan 1st 1970) and just do normal subtraction.

    Be careful of leap years though – and things get even more fun if you need to cope with the bizarre calendar changes a few centuries back.

    Sample code assuming a format of “yyyyMMdd”, not doing any error checking, not coping with dates before 1900, and not worrying about efficiency at all:

    using System;
    
    struct Date
    {
        readonly int year;
        readonly int month;
        readonly int day;
    
        public Date(int year, int month, int day)
        {
            this.year = year;
            this.month = month;
            this.day = day;
        }
    
        public static Date Parse(string text)
        {
            return new Date(int.Parse(text.Substring(0, 4)),
                            int.Parse(text.Substring(4, 2)),
                            int.Parse(text.Substring(6, 2)));
        }
    
        // Days since first Jan 1st 1900
        public int DaysSinceEpoch
        {
            get
            {
                int days = 0;
                for (int i = 1900; i < year; i++)
                {
                    days += IsLeapYear(i) ? 366 : 365;
                }
                for (int i = 1; i < month; i++)
                {
                    days += GetMonthLength(i, year);
                }
                days += day - 1;
                return days;
            }
        }
    
        private static readonly int[] MonthLengths = 
        { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    
        private static int GetMonthLength(int month, int year)
        {
            return MonthLengths[month-1] + 
                ((IsLeapYear(year) && month == 2) ? 1 : 0);
        }
    
        private static bool IsLeapYear(int year)
        {
            // http://en.wikipedia.org/wiki/Leap_year
            if ((year % 4) != 0) return false;
            if ((year % 400) == 0) return true;
            if ((year % 100) == 0) return false;
            return true;
        }
    }
    
    class Test
    {
        static void Main(string[] args)
        {
            Console.WriteLine(DateDiff("19040301", "19050301")); // 365
            Console.WriteLine(DateDiff("19040201", "19050201")); // 366
            Console.WriteLine(DateDiff("19760619", "20090529")); // I feel old
        }
    
        static int DateDiff(string first, string second)
        {
            Date firstDate = Date.Parse(first);
            Date secondDate = Date.Parse(second);
            return secondDate.DaysSinceEpoch - firstDate.DaysSinceEpoch;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Have you looked at .wrapInner()? It will allow you to… May 11, 2026 at 9:45 pm
  • Editorial Team
    Editorial Team added an answer Use load-time weaving. http://www.aspectprogrammer.org/blogs/adrian/2004/05/loadtime_weavin.html Here's how to do it in… May 11, 2026 at 9:45 pm
  • Editorial Team
    Editorial Team added an answer You can use getattr: >>> class helloworld: ... def world(self):… May 11, 2026 at 9:45 pm

Related Questions

In fact i am trying to calculate the time a function takes to complete
Can anyone tell me how to add an AssemblyInfo.vb file to a Web Application
Can anyone tell me how to write a nested SQL query like SELECT *
Can anyone tell me how to quickly copy Files from a mapped network drive?
Can anyone tell me how to work with the parameters stored in the value

Trending Tags

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

Top Members

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.