我该怎么做 [英] How can i do this functionality

查看:62
本文介绍了我该怎么做的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

我具有像在日期之间检索值的功能.

hi every body

i have a functionlity like retrieve values between dates.

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........

推荐答案

只是为了让您有个想法,请使用此代码.它检查item3和item4是否在2012年.
Just to get you an idea use this code. It checks if items3 and item4 are in 2012 or not in 2012.
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");
      }
   }
}


正则表达式类的IsMatch方法可用于此目的,如下所示:

The IsMatch method of Regex class can be used for this purpose as shown below:

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


这篇关于我该怎么做的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆