Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
hi every body

i have a functionlity like retrieve values between dates.

C#
string item1 = "01/01/2012";
string item2 = "01/31/2012";
string item3 = "03/06/2012";
string item4 = "03/06/2011";

if (item1 != String.Empty && item2 != String.Empty && item3 != String.Empty && item4 != String.Empty)
{
    if (item1 > item3 && item1 < item2)
    {
        //adding only between 01/01/2012 to 12/31/2012 values here......(item3 come here)
    }
    else
    {
        //adding only out of 2012 year values here......(item4 come here)
    }
}

//How can i do this Can you any body idea this


thanks in advance........
Posted
Updated 7-Jun-12 18:40pm
v3

Just to get you an idea use this code. It checks if items3 and item4 are in 2012 or not in 2012.
C#
public void InsertDates()
{
   string item1 = "01/01/2012";
   string item2 = "01/01/2013";
   string item3 = "03/06/2012";
   string item4 = "03/06/2011";
   string item5 = "03/06/2013";

   if (item1 != String.Empty && item2 != String.Empty && item3 != String.Empty && item4 != String.Empty && item5 != String.Empty)
   {
      DateTime tm1 = Convert.ToDateTime(item1);
      DateTime tm2 = Convert.ToDateTime(item2);
      DateTime tm3 = Convert.ToDateTime(item3);
      DateTime tm4 = Convert.ToDateTime(item4);
      DateTime tm5 = Convert.ToDateTime(item5);
      if (tm3 > tm1 && tm3 < tm2)
      {
         MessageBox.Show("item3 is in 2012");
      }
      else
      {
         MessageBox.Show("item3 is not in 2012");
      }
      if (tm4 > tm1 && tm4 < tm2)
      {
         MessageBox.Show("item4 is in 2012");
      }
      else
      {
         MessageBox.Show("item4 is not in 2012");
      }
      if (tm5 > tm1 && tm5 < tm2)
      {
         MessageBox.Show("item5 is in 2012");
      }
      else
      {
         MessageBox.Show("item5 is not in 2012");
      }
   }
}
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 8-Jun-12 0:36am    
Good point here is working with dates, not string representing strings. I bet those four strings could not be created in first place, in reasonable design.
My 5.
--SA
U@007 8-Jun-12 0:36am    
Hi jf,
i know this validation here when i convert string to datetime it will taken date with time that is "01/01/2012 08:20:55 PM".
So, it's again not validate here.......

that's y iam asking.
JF2015 8-Jun-12 0:40am    
Have a look at the MSDN documentation for the Convert.ToDateTime function:
http://msdn.microsoft.com/en-us/library/9xk1h71t
If your strings to convert to datetime you may need a special IFormatProvider. Work through the documentation - this will help you.
U@007 8-Jun-12 0:49am    
Thank u jf.

this link is very nice.

in my side 5+.
VJ Reddy 8-Jun-12 5:07am    
Good alternative.
It is better to work in the original format data. 5!
The IsMatch method of Regex class can be used for this purpose as shown below:

C#
string item1 = "01/01/2012";
string item2 = "01/01/2012";
string item3 = "03/06/2012";
string item4 = "03/06/2011";

string[] dates = new string[]{item1,item2,item3,item4};
string year2012=string.Empty;
string otherYears = string.Empty;
foreach (string date in dates)
{
    if (Regex.IsMatch(date,@".*/\s*2012", RegexOptions.CultureInvariant))
        year2012 += string.Format("{0}{1}",
                        string.IsNullOrEmpty(year2012)? "" : ", ",date);
    else
        otherYears += string.Format("{0}{1}",
                        string.IsNullOrEmpty(otherYears)? "" : ", ",date);

}
Console.WriteLine (year2012);
Console.WriteLine (otherYears);

//Output
//01/01/2012, 01/01/2012, 03/06/2012
//03/06/2011
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 8-Jun-12 0:37am    
Sorry, but this is not a good approach -- still working with strings, not dates.
Please see the answer by JF2015 and my comment to it.
(I did not vote this time.)
--SA
VJ Reddy 8-Jun-12 2:14am    
Thank you, SA, for the comment.

I thought of converting to DateTime.
But, dropped the idea, as the OP wants to add dates of 2012 year into one group and all other remaining dates into another group, which can be achieved easily using Regex, without involving the overhead of conversion.

I do agree with you, the Conversion to DateTime would be useful, efficient and preferred approach if any other operation of DateTime is required.

Thank you :)
JF2015 8-Jun-12 0:40am    
Although I prefer to convert the dates to the DateTime object, I really like your approach and I hate that I didn't think of it :) 5+
VJ Reddy 8-Jun-12 2:15am    
Thank you very much, JF2015 :)
U@007 8-Jun-12 0:50am    
thank you reddy.
5+

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