Click here to Skip to main content
15,888,330 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
they have two timings like 10:00 and 12.00 in date time format how to check these time range between other two timings in c#like 10:00 and 12:30(use date time format)
Posted
Comments
Mitchell J. 28-May-14 6:49am    
What do you mean by "check" - see if the hour component of them is equal maybe?
You need to provide more detail before we can help.
KaushalJB 28-May-14 6:51am    
So whats the issue ?? Where is your code ? and Who "They" ??? Please read Help for How to submit question...
sailaja12345672 28-May-14 6:55am    
check givien two timings(12:00-13:00) betweent two timinings(12:30&&13:00)
Prasad Avunoori 28-May-14 7:24am    
Please make it clear.

Essentially you are trying to determine whether a given time range falls between a fixed time range. This sounds like homework so I'm not going to give you the full solution, but hopefully this will be enough to get you going.

You need to convert each time into a timespan - reference[^] i.e. you will end up with 4 timespan variables which I will call bottomOfRange, topOfRange, bottomOfTarget and topOfTarget

First create yourself a helper function that will compare just one timespan with the fixed range ...e.g.
C#
bool isTimeBetween(TimeSpan InTime, TimeSpan bottomOfTarget, TimeSpan topOfTarget)
{
    // Validate the parameters
    if (bottomOfTarget> topOfTarget)
        throw new Exception("Start of range must be before end of range");

    // Check time is within the range
    if (InTime < bottomOfTarget|| InTime > topOfTarget)
        return false;
    else
        return true;
}

Then you can call that from another function that takes both of the timespans
C#
bool isRangeInRange(TimeSpan bottomOfRange, TimeSpan topOfRange, TimeSpan bottomOfTarget, TimeSpan topOfTarget)
{
    // I've left this for you to implement
}

Then you call the second function e.g.
C#
if (isRangeInRange(bottomOfRange, topOfRange, bottomOfTarget, topOfTarget))
    MessageBox.Show("values are in range");
else
    MessageBox.Show("values are not in range");


You've mentioned using a datetime but elsewhere are just using times, however to convert the time part of a datetime to a timespan you could use
DateTime dt1 = DateTime.Now;
bottomOfRange = new TimeSpan(dt1.Hour, dt1.Minute, dt1.Second);
 
Share this answer
 
You are trying to check if 2 time ranges overlap one another, right? Try this:
using System;
using System.Globalization;
 
public class Program
{
    public static void Main()
    {
        DateTime timeFrom = new DateTime(2014,05,28,10,0,0); 
		DateTime timeTo = new DateTime(2014,05,28,12,30,0); 
		
		DateTime timeLeftBorder = new DateTime(2014,05,28,10,0,0);  
		DateTime timeRightBorder = new DateTime(2014,05,28,12,30,0);  
		
		bool isTimeOverlapped = !((timeFrom >= timeLeftBorder && timeFrom<= timeRightBorder) && (timeTo >= timeLeftBorder && timeTo<= timeRightBorder));
		

        Console.WriteLine(isTimeOverlapped);  
    
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900