Blue Theme Orange Theme Green Theme Red Theme
 
MindFusion's Components
Skip Navigation Links
C# Corner Home
Forum Home
Latest 50
Unanswered
Win $500 Cash
All Time Leaders
Jump to CategoryExpand Jump to Category
Login 
    Welcome Guest!
 Search Forum For :  
X
 Login
Please login to submit a new post, reply and edit exiting posts, see user profiles, and access more features. If you are not a registered member, Register here.
User Id:
Password:  
Forgot Password | Forgot UserName
   Home » C# Language » Intersection of date time intervals
       
Author Reply
egi zaberl
posted 2 posts
since Jul 25, 2005 
from

 Intersection of date time intervals
  Posted on: 10/26/2008 9:50:42 AM       
Dear all,

I want to get time value out from intersection of two time intervals.

Example:
Main time interval: 09:20:00 - 15:00:00
Time interval 1: 06:00:00 - 14:00:00
Time interval 2: 14:00:00 - 22:00:00

Intersection of main time interval and interval 1 is 04:40:00
Intersection of main time interval and interval 2 is 01:00:00

How to do that in C#?

Thank you!

Regards, Egi
Alan
posted  1600 posts
since  Jun 02, 2007 
from 

 Re: Intersection of date time intervals
  Posted on: 10/26/2008 10:41:06 AM       

This should work though, if the intervals straddle midnight, then you'll need to specify the date as well as the time:

using System;

class Program
{
    static void Main()
    {
        DateTime mainStart = DateTime.Parse("09:20:00");
        DateTime mainEnd = DateTime.Parse("15:00:00");

        DateTime intervalStart = DateTime.Parse("06:00:00");
        DateTime intervalEnd = DateTime.Parse("14:00:00");

        TimeSpan ts = GetIntersection(mainStart, mainEnd, intervalStart, intervalEnd);
        Console.WriteLine(ts);

        intervalStart = DateTime.Parse("14:00:00");
        intervalEnd = DateTime.Parse("22:00:00");        
        ts = GetIntersection(mainStart, mainEnd, intervalStart, intervalEnd);
        Console.WriteLine(ts);

        Console.ReadLine();       
    }

    public static TimeSpan GetIntersection(DateTime mainStart, DateTime mainEnd, DateTime intervalStart, DateTime intervalEnd)
    {
        if (mainStart >= mainEnd || intervalStart >= intervalEnd)
        {
            return TimeSpan.Zero;
        }
 
        if(intervalStart >= mainEnd || intervalEnd <= mainStart)
        {
            return TimeSpan.Zero;
        }

        if (intervalStart >= mainStart && intervalEnd <= mainEnd)
        {
            return intervalEnd - intervalStart;
        }

        DateTime tempStart = intervalStart;
        DateTime tempEnd = intervalEnd;

        if (intervalStart < mainStart) tempStart = mainStart;
        if (intervalEnd > mainEnd) tempEnd = mainEnd;
        return tempEnd - tempStart;    
    }
}

Alan
posted  1600 posts
since  Jun 02, 2007 
from 

 Re: Intersection of date time intervals
  Posted on: 10/26/2008 10:58:30 AM   Accepted Answer    

I've modified the above program so that, if you enter a negative or zero interval, it assumes that the interval must end on the following day i.e. 24 hours should be added:

using System;

class Program
{
    static void Main()
    {
        DateTime mainStart = DateTime.Parse("09:20:00");
        DateTime mainEnd = DateTime.Parse("05:00:00");

        DateTime intervalStart = DateTime.Parse("06:00:00");
        DateTime intervalEnd = DateTime.Parse("05:40:00");

        TimeSpan ts = GetIntersection(mainStart, mainEnd, intervalStart, intervalEnd);
        Console.WriteLine(ts);

        intervalStart = DateTime.Parse("14:00:00");
        intervalEnd = DateTime.Parse("01:00:00");        
        ts = GetIntersection(mainStart, mainEnd, intervalStart, intervalEnd);
        Console.WriteLine(ts);

        Console.ReadLine();       
    }

    public static TimeSpan GetIntersection(DateTime mainStart, DateTime mainEnd, DateTime intervalStart, DateTime intervalEnd)
    {
        if (mainStart >= mainEnd)
        {
            mainEnd += new TimeSpan(24,0,0);
        }

        if (intervalStart >= intervalEnd)
        {
            intervalEnd += new TimeSpan(24,0,0);
        }
    
        if(intervalStart >= mainEnd || intervalEnd <= mainStart)
        {
            return TimeSpan.Zero;
        }

        if (intervalStart >= mainStart && intervalEnd <= mainEnd)
        {
            return intervalEnd - intervalStart;
        }

        DateTime tempStart = intervalStart;
        DateTime tempEnd = intervalEnd;

        if (intervalStart < mainStart) tempStart = mainStart;
        if (intervalEnd > mainEnd) tempEnd = mainEnd;
        return tempEnd - tempStart;    
    }
}

egi zaberl
posted  2 posts
since  Jul 25, 2005 
from 

 Re: Intersection of date time intervals
  Posted on: 10/26/2008 4:16:54 PM       
Thank you for very quick response!
I'll try that piece of code later and let u know if this is what i was looking for ...

       
Developer-Ready ASP.NET 2.0 Web Hosting with 3 MONTHS FREE
Now supporting .NET 3.0 Framework with Windows Workflow Foundation, Windows Communication Foundation (WCF), Windows Presentation Foundation (WPF), windows CardSpace (WCS)! Providing more flexibility for Developers with Web Services Support and a User/Permission Manger. Also supporting MS SQL 2005/2000 with Real-Time Backups, FREE Automated Attach .MDF Tool, FREE SQL Restore and Shrink SQL DB Tools, and SQL
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Clickatell's SMS Gateway
Clickatell's Developer Solutions allow you to SMS enable any website or application via a range of API's. Learn More about our API connections.
Free access to .NET Memory Management video
Everything you need to know about Garbage Collection, Temporary Objects, Fragmentation, Finalization and common causes of memory leaks in .NET. Watch the video here.
Microsoft Visual Studio 2010
Microsoft Visual Studio 2010 offers more to developers than any other Visual Studio release. Work more productively and collaboratively-with greater control over your work at every step. The Beta 2 can give you a head start on achieving efficiency.

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Advertise with us
Current Version: 3.2009.8.27
 © 1999 - 2009  Mindcracker LLC. All Rights Reserved