给定任意时间段,输出这个时间段的每一天日期,最后存入字符串数组中
例如:2008-5-5到2008-6-6
那么输出应该是这样的:
2008-5-5
2008-5-6
。。
2008-5-31
2008-6-1
2008-6-2
2008-6-3
2008-6-4
2008-6-5
2008-6-6最后存入字符串数组中下面是用来写这个算法的空方法public string[] TimeArray(DateTime startDateTime, DateTime endDateTime)
{}
请高手赐教!

解决方案 »

  1.   

    datetime.addday(1)边循环,边判断,边添加到返回的数组中
      

  2.   


    string strStartdate = "2008-5-5";
    string strEnddate = "2008-6-6"; DateTime startDate = DateTime.ParseExact(strStartdate, "yyyy-M-d", null);
    DateTime endDate = DateTime.ParseExact(strEnddate, "yyyy-M-d", null); DateTime thisDate = startDate; while (thisDate <= endDate)
    {
    Console.WriteLine(thisDate.ToString("yyyy-M-d"));
    thisDate = thisDate.AddDays(1);
    }
      

  3.   

    参1#for(datetime day = startDateTime.addday(1); day != endDateTime; datetime.addday(1))
    {
        //..这里不包含有startDateTime和endDateTime
    }
      

  4.   

    strBegin = "2008-5-5";
    strEnd = "2008-6-6";
    DateTime tBegin = DateTime.Parse(strBegin);
    DateTime tEnd = DateTime.Parse(strEnd);
    for (DateTime t = tBegin; t <= tEnd; t = t.AddDays(1))
    {
        Console.WriteLine(t.ToString());
    }没有测试过,思路应该就是这样吧。
      

  5.   

    TimeSpan span = endDateTime - startDateTime;
    int days = Convert.ToInt32(Math.Ceiling(span.TotalDays));days表示是总共有多少天吧
      

  6.   

    按照你的函数来就是这样:
    public string[] TimeArray(DateTime startDateTime, DateTime endDateTime)
    {
                    TimeSpan s=endDateTime.Subtract(startDateTime);
    int i=s.Days;
    string[] sDate=new string[i+1];
    DateTime thisDate = startDateTime;
    for(int j=0;j<=i;j++)
    {
                sDate[j]=thisDate.ToShortDateString();
    thisDate = thisDate.AddDays(1);
    }
                return  sDate;
    }
      

  7.   


    public string[] TimeArray(DateTime startDateTime, DateTime endDateTime)
    {
    DateTime thisDate = startDateTime; List<string> dateList = new List<string>(); while (thisDate <= endDateTime)
    {
    dateList.Add(thisDate.ToString("yyyy-M-d"));
    thisDate = thisDate.AddDays(1);
    } return dateList.ToArray();
    }