三个时间对比取哪个最大
如:
2011/4/17 13:56:29
2013/1/17 13:56:29
2011/6/18 13:56:29
第二个大 
        DateTime d1 = DateTime.Now.AddDays(1).AddMonths(1);
        DateTime d2 = DateTime.Now.AddDays(1).AddMonths(22);
        DateTime d3 = DateTime.Now.AddDays(2).AddMonths(3);        if (d1 > d2 &&d1> d3)
        {
            Response.Write("第一个大");
        }
        else
        {
            if (d2 > d3 && d2 > d1)
            {
                Response.Write("第二个大");
            }
            else
            {
                if (d3 > d1 && d3 > d2)
                {
                    Response.Write("第三个大");
                }
            }
        }不要用这种写法,这种时间多了判断太多了

解决方案 »

  1.   


    if(d1 > d2)
    {
     if(d1 > d3)
      {
        d1最大
      }
      else
     {
      d3最大
     }
    }
    else
    {
      if(d2 > d3)
      {
        d2最大 
      }
      else
      {
       d3 最大
      }
    }
      

  2.   

    从网上搜个demo看看就知道了。简单的很
      

  3.   

    //其实这个已经比什么都快了        DateTime d1 = DateTime.Now.AddDays(1).AddMonths(1);
            DateTime d2 = DateTime.Now.AddDays(1).AddMonths(22);
            DateTime d3 = DateTime.Now.AddDays(2).AddMonths(3);        if (d1 > d2 &&d1> d3)
            {
                Response.Write("d1大");
            }
            else
            {
                if (d2 > d3)
                {
                    Response.Write("d2大");
                }
                else
                {
                    Response.Write("d3大");
                }
            }
      

  4.   

    一句话就判断出来了: DateTime d1 = DateTime.Now.AddDays(1).AddMonths(1);
                DateTime d2 = DateTime.Now.AddDays(1).AddMonths(22);
                DateTime d3 = DateTime.Now.AddDays(2).AddMonths(3);
                Console.WriteLine("d1、d2、d3 中{0}最大",d1==d2?(d2==d3?"d1、d2、d3":(d2>d3?"d1、d2":"d3")):( d1>d2?((d1>d3?"d1":"d3")):((d2<d3?"d3":"d2"))));
      

  5.   

    上面格式乱了:Console.WriteLine("d1、d2、d3 中{0}最大",d1==d2?(d2==d3?"d1、d2、d3":(d2>d3?"d1、d2":"d3")):( d1>d2?((d1>d3?"d1":"d3")):((d2<d3?"d3":"d2"))));
      

  6.   

    //取最大时间
    datatime MaxD=(d1>=d2&&d1>=d3?d1:(d2>=d3?d2:d3));
      

  7.   

            private static void Timecompare()
            {
                string[] strc = "2011/4/17 13:56:29|2013/1/17 13:56:29|2011/6/18 13:56:29".Split(new char[] {'|'});
                for(int i=0;i<strc.Length;i++)
                {
                    for(int j=0;j< i+1;j++)
                    {   
                    if( Convert.ToDateTime(strc[i])> Convert.ToDateTime(strc[j]))
                     {
                        string  stemp =strc[j];
                        strc[j] = strc[i];
                        strc[i] = stemp;
                     }
                    }
                }
                foreach (string sr in strc)
                {
                    Console.WriteLine(sr);
                    
                }
                Console.ReadKey();
             }沿用冒泡排序即可
      

  8.   

    var dts=new list<datetime>[]{d1,d2,d3};
    var maxdt=from dt in dts
              orderby dt
              select max(dt);
      

  9.   

    DateTime d1 = DateTime.Now.AddDays(1).AddMonths(1);
                DateTime d2 = DateTime.Now.AddDays(1).AddMonths(22);
                DateTime d3 = DateTime.Now.AddDays(2).AddMonths(3);            DateTime[] D = { d1, d2, d3 };
                int a = 0;
                for (int i = 1; i < 3; i++)
                {
                    if (D[a] < D[i])
                    {
                        a = i;
                    }
                }
                Console.Write("第" + a + "个大");
      

  10.   

                DateTime d1 = DateTime.Now.AddDays(1).AddMonths(1);
                DateTime d2 = DateTime.Now.AddDays(1).AddMonths(22);
                DateTime d3 = DateTime.Now.AddDays(2).AddMonths(3);            DateTime[] D = { d1, d2, d3 };
                int a = 0;
                for (int i = 1; i < 3; i++)
                {
                    if (D[a] < D[i])
                    {
                        a = i;
                    }
                }
                a++;
                Console.Write("第" + a + "个大");
      

  11.   

    DateTime[] D = { d1, d2, d3 };
      int a = 0;
      for (int i = 1; i < 3; i++)
      {
      if (D[a] < D[i])
      {
      a = i;
      }
      }
      a++;a代表着D(一组日期)的索引位置,
     if (D[a] < D[i])
      {
      a = i;
      }
    一开始D[a]是一组日期的第一个,因为a=0,然后判断它与这一组日期,对比,如果这个日期小于与他比较的日期,在讲这个日期修改成当前这个日期,继续比较,这个写法需要优化,请稍等
      

  12.   

    max=d1;
    if(max<d2)max=d2;
    if(max<d3)max=d3;
    max是最大的!
      

  13.   

    因为数组的第一项是从0开始的
    D[0]表示d1
    D[1]表示d2
    D[2]表示d3
      

  14.   

    这种问题为什么不用集合做?3.0以上版本...
    var datelist = new DateTime[] { d1, d2, d3 };
    Console.WriteLine(datelist.Max());
    3.0以下版本...
    DateTime[] datelist = new DateTime[] { d1, d2, d3 };
    Array.Sort(datelist);
    Console.WriteLine(datelist[datelist.Length - 1]);
      

  15.   

    Console.WriteLine(datelist.Max());
    原来这么一句就可以了,螃蟹V5
      

  16.   

       DateTime d1 = DateTime.Now.AddDays(1).AddMonths(1);
            DateTime d2 = DateTime.Now.AddDays(1).AddMonths(22);
            DateTime d3 = DateTime.Now.AddDays(2).AddMonths(3);        DateTime[] dtlist = { d1, d2, d3 };
            DateTime tmpMaxTime;        if (dtlist!=null && dtlist.Length > 0)
            {
                int iTimePosition = 1;
                tmpMaxTime = dtlist[iTimePosition];//
                for (int i = 1; i < dtlist.Length; i++)
                {
                    if (tmpMaxTime < dtlist[i])
                    {
                        tmpMaxTime = dtlist[i];
                        iTimePosition = i+1;
                        continue;
                    }
                }
                //iTimePosition 最大日期的顺序
                //哈哈,为了星,没办法
                //未测试
            }
      

  17.   


    public class test { /**
     * @param args
     * @throws ParseException
     */
    public static void main(String[] args) throws ParseException {
    int num = compareDate("20100201", "20110110", "yyyyMMdd");
    System.out.println(num);
    } public static int compareDate(String day1, String day2, String strFormat)
    throws ParseException { Date date1 = getFormatDate(day1, strFormat);
    Date date2 = getFormatDate(day2, strFormat);
    return date1.compareTo(date2);
    } public static Date getFormatDate(String strDate, String strFormat)
    throws ParseException {
    if (isEmpty(strDate) || isEmpty(strFormat)) {
    return null;
    } SimpleDateFormat df = new SimpleDateFormat(strFormat);
    df.setLenient(false);
    return df.parse(strDate);
    } /**
     * 判断字符串是否为空
     * 
     * @param strVal
     *            string
     * @return true 为空 false 不为空
     */
    public static boolean isEmpty(String strVal) {
    return strVal == null || trim(strVal).equals("");
    } /**
     * 去除左右边全、半角空格
     * 
     * @param strValue
     *            字符串
     * @return String 去除左右边全、半角空格字符串
     */
    public static String trim(String strValue) { if ((strValue != null) && (!strValue.equals(""))) {
    StringBuffer sb = new StringBuffer();
    StringBuffer sb1 = new StringBuffer();
    String strRet = "";
    sb.append(rTrim(strValue));
    // 反转后去掉右空格
    strRet = rTrim(sb.reverse().toString());
    sb1.append(strRet);
    strRet = sb1.reverse().toString();
    return strRet.trim();
    } else {
    return "";
    }
    } /**
     * 去除右边全半角空格
     * 
     * @param strValue
     *            String
     * @return String String
     */
    private static String rTrim(String strValue) { if ((strValue != null) && (!strValue.equals(""))) {
    char[] cValue = strValue.toCharArray();
    int nCur = 0; for (int i = cValue.length - 1; i > -1; i--) {
    if ((cValue[i] != '\u0020') && (cValue[i] != '\u3000')) {
    nCur = i;
    break;
    }
    } if ((nCur == 0)
    && ((cValue[0] == '\u0020') || (cValue[0] == '\u3000'))) {
    return "";
    } return strValue.substring(0, nCur + 1);
    } else {
    return "";
    }
    }}
      

  18.   


    public static void main(String[] args) throws ParseException {
    String[] arr = {"2011/4/17 13:56:29", "2013/1/17 13:56:29",
    "2011/6/18 13:56:29"};
    String maxDate = compareThreeDate(arr, "yyyy/MM/dd HH:mm:ss");
    System.out.println(maxDate);
    } public static String compareThreeDate(String[] arr, String strFormat)
    throws ParseException {
    String date = "";
    if(arr!=null&&arr.length>1){
    date = arr[0];
    for (int i = 1; i < arr.length; i++) {
    int num = compareDate(date, arr[i], strFormat);
    if (num < 0) {
    date =  arr[i];
    }
    }
    }

    return date;
    } /**
     * 比较 两个时间的大小
     * 
     * @param day1
     * @param day2
     * @param strFormat
     * @return day1>day2 返回1 day1=day2 返回0 day1<day2 返回-1
     * @throws ParseException
     */
    public static int compareDate(String day1, String day2, String strFormat)
    throws ParseException { Date date1 = getFormatDate(day1, strFormat);
    Date date2 = getFormatDate(day2, strFormat);
    return date1.compareTo(date2);
    } public static Date getFormatDate(String strDate, String strFormat)
    throws ParseException {
    if (isEmpty(strDate) || isEmpty(strFormat)) {
    return null;
    } SimpleDateFormat df = new SimpleDateFormat(strFormat);
    df.setLenient(false);
    return df.parse(strDate);
    } /**
     * 判断字符串是否为空
     * 
     * @param strVal
     *            string
     * @return true 为空 false 不为空
     */
    public static boolean isEmpty(String strVal) {
    return strVal == null || "".equals(strVal.trim());
    }