有三个日期值。想返回与小于当前日期,且于当前日期最接近的日期。昨天没有看清题,所以,写了个求最大值的方法 private string CompDate(string strDate1, string strDate2, string strDate3)
{
if (Convert.ToDateTime(strDate1) >= Convert.ToDateTime(strDate2))
{
if (Convert.ToDateTime(strDate1) >= Convert.ToDateTime(strDate3))
{
return m_strRecordName1;
}
else
{
return m_strRecordName3;
}
}
else
{
if (Convert.ToDateTime(strDate2) >= Convert.ToDateTime(strDate3))
{
return m_strRecordName2;
}
else
{
return m_strRecordName3;
}
}
}希望高手帮忙。

解决方案 »

  1.   

    不笨.没用过而已.使用TimeSpan.来查看两哥时间之间的时间差.具体的可以查一下msdn.或搜一下就知道了.
      

  2.   

    private string CompDate(DateTime strDate1, DateTime strDate2, DateTime strDate3)
    {
    TimeSpan ts;
    DateTime result;
    if (strDate1< DateTime.Now)
    {
    ts=DateTime.Now-strDate1;
    result=strDate1;
    }
    if (strDate2< DateTime.Now)
    {
    if (ts>DateTime.Now-strDate2)
    {
    ts=DateTime.Now-strDate2;
    result=strDate2;
    }
    }
    if (strDate3< DateTime.Now)
    {
    if (ts>DateTime.Now-strDate3)
    {
    ts=DateTime.Now-strDate3;
    result=strDate3;
    }
    }
    return result;
    }
      

  3.   

    public DateTime GetTime(DateTime[] date)
            {
                DateTime now = DateTime.Now;
                DateTime value = new DateTime(1);
                for (int i = 0; i < date.Length; i++)
                {
                    if (date[i].Ticks < now.Ticks && ((now.Ticks - date[i].Ticks) < (now.Ticks - value.Ticks)))
                    {
                        value = date[i];
                    }
                }
                if (value == (new DateTime(1)))
                {
                    throw new Exception("没有比当前日期更小的日期");
                }
                else
                {
                    return value;
                }
            }
      

  4.   

    private string CompDate(string strDate1, string strDate2, string strDate3)
    {
    DateTime cur = DateTime.Now;
    DateTime[] d = new DateTime[]{DateTime.Parse(strDate1),DateTime.Parse(strDate2) ,DateTime.Parse(strDate3),cur};            
    Array.Sort(d,0,d.Length,null);//这是关键
    for(int i =0;i<d.Length;i++)
    {
    if(d[i] == cur && i != 0)//如果当前日期不是最小的日期,就找到答案啦
    {
    return d[i-1].ToString();
    }
    }
    return "";//如果没找到,则返回空
    }
      

  5.   

    呵呵,解决问题了:) 如果有不对的,希望能帮我改:)
    感谢大家。 private string CompDate(DateTime[] dtDate)
    {
    DateTime dtNow = DateTime.Now;
    DateTime dtRet = new DateTime(1); for (int i = 0; i < dtDate.Length; i++)
    {
    if (dtDate[i].Ticks <= dtNow.Ticks && ((dtNow.Ticks - dtDate[i].Ticks) <= (dtNow.Ticks - dtRet.Ticks)))
    {
    dtRet = dtDate[i];
    }
    }

    int nCount = 0;
    for (int j = 0 ; j < dtDate.Length ; j++)
    {
    if (dtRet == dtDate[j])
    {
    nCount = j;
    break;
    }
    } return m_strRecordName + (nCount+1).ToString();
    }
      

  6.   

    唉。看不明白你写的什么。
    你为什么不看别人的回复呢,自己去写出这么麻烦的东西。
    private string CompDate(DateTime[] dtDate)
    {
    DateTime cur = DateTime.Now;
    Array.Sort(dtDate,0,dtDate.Length,null);
    for(int i =0;i<d.Length;i++)
    {
    if(d[i] == cur && i != 0)
    {
    return d[i-1].ToString();//小于等于当前日期的最大日期
    }
    }
    return cur.ToString();//如果没有找到小于当前日期的,则返回当前日期
    }
    思路:将数组排序,你要取大于,等于,小于当前日期的,不是很方便了吗?这是根据我上面的方法改的,只要知道了原理,怎么修改都可以。
      

  7.   

    不好意思。没有将d替换为dtDate.
      

  8.   

    MyLf(不睡觉的鱼) 说的不错,可以先排序,然后再比较,这样比较好。