Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
Hi All,

I have dates:-

02/11/2011
25/12/2011
09/12/2011
13/12/2011

and I have to check these dates one by one between 01/11/2011 and 15/12/2011.

If any one of above dates lies between given date than it should return a value showing date existing and non-existing e.g(1 for non-existing and 0 for non-existing ) and loop should not move more . please help me how to do it.

Thanks

Mohd wasif
Posted
Updated 11-Mar-12 18:39pm
v2

List<DateTime> list = new List<DateTime> { Convert.ToDateTime("02/11/2011"), Convert.ToDateTime("25/12/2011"), Convert.ToDateTime("09/12/2011"), Convert.ToDateTime("13/12/2011") };


        foreach(var date in list)
        {
            if ((date.CompareTo(Convert.ToDateTime("01/11/2011")) > 0) && (date.CompareTo(Convert.ToDateTime("15/12/2011")) < 1))
            {
                //return value
                break;
            }

        }
 
Share this answer
 
Comments
Ankit Gajera 22-Oct-13 8:23am    
How you Can Compare the perticular date 01/11/2011,15/12/2011
D.M. Abdur Rahim 17-May-16 3:45am    
((date.CompareTo(Convert.ToDateTime("01/11/2011")) >= 0) && (date.CompareTo(Convert.ToDateTime("15/12/2011")) < 1))
C#
protected void FindDate()
    {
        DateTime d1 = new DateTime(2011, 11, 2);
        
        DateTime StartDate = new DateTime(2011, 11, 01);
        DateTime EndDate = new DateTime(2011, 12, 15);
        int DayInterval = 1;

        List<datetime> dateList = new List<datetime>();
        while (StartDate.AddDays(DayInterval) <= EndDate)
        {
            StartDate = StartDate.AddDays(DayInterval);
            dateList.Add(StartDate);
            if (StartDate == d1)
            {
                Response.Write(d1.ToString() + " Exist");
            }
        }        
    }
 
Share this answer
 
v2
Comments
hemu2123 11-Jan-13 7:47am    
how to get all date between two dates
for ex i selacted two date 10-01-2013 and 10-02-2013
i want to all dates between these two dates
Mukund Thakker 11-Jan-13 7:59am    
You are having all the dates between 2 dates is in StartDate variable.
hemu2123 11-Jan-13 8:03am    
yes i want to show all date bteween start date and end date
Mukund Thakker 11-Jan-13 8:09am    
Write following code to get all the dates between 2 dates
while (StartDate.AddDays(DayInterval) <= EndDate)
{
StartDate = StartDate.AddDays(DayInterval);
Response.Write(StartDate.ToString());
}
hemu2123 11-Jan-13 8:12am    
thanku sir
if i insert date with the help of textbox then we first calculate day interval???
Oh!..

C#
bool InBetween(
    ref System.DateTime toTest, 
    ref System.DateTime start,
    ref System.DateTime end) {
        return (start < toTest) && (toTest < end);
        //or, depending on how you define "between":
        return (start <= toTest) && (toTest <= end);
}


As parameter type is a value type, data is passed by reference for the sake of performance, but none of the parameters is actually modified through its reference. If you remove "ref", the code will give the same results.

In other words, the type System.DateTime supports comparison and order relationship using the customary comparison operators. This is the most readable and maintainable way of performing comparison. Please see:
http://msdn.microsoft.com/en-us/library/system.datetime.aspx[^].

—SA
 
Share this answer
 
v2
You can use the Time Period Library for .NET[^]:
C#
// ----------------------------------------------------------------------
public void CheckDateBetweenDatesSample()
{
  DateTime[] moments = new[]
    {
      new DateTime( 2011, 11, 02 ),
      new DateTime( 2011, 12, 25 ),
      new DateTime( 2011, 12, 09 ),
      new DateTime( 2011, 12, 13 )
    };

  TimeRange timeRange = new TimeRange( 
    new DateTime( 2011, 11, 01 ), 
    new DateTime( 2011, 12, 15 ) );

  Console.WriteLine( "time range: {0}", timeRange );
  foreach ( DateTime moment in moments )
  {
    Console.WriteLine( "{0:d} is inside: {1}", 
      moment, 
      timeRange.HasInside(moment) ? "1" : "0" );
  }
} // CheckDateBetweenDatesSample
 
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