0

我想提取当月的所有星期日并拥有以下代码:

 private string GetDatesOfSundays(DateTime DatMonth)
 {
  string sReturn = "";
  int iDayOffset = DatMonth.Day - 1;
  DatMonth = DatMonth.AddDays(System.Convert.ToDouble(-DatMonth.Day + 1));
  DateTime DatMonth2 = DatMonth.AddMonths(1).AddDays(System.Convert.ToDouble(-1));
  while (DatMonth < DatMonth2)
  {
    if (DatMonth.DayOfWeek == System.DayOfWeek.Sunday)
    {
      if (sReturn.Length > 0) sReturn += ",";
      sReturn += DatMonth.ToShortDateString();
    }
    DatMonth = DatMonth.AddDays(1.0);
  }
  return sReturn;
}   

[HttpGet]
public ActionResult TradeUKKPISearchesData()
{
  string allSundaysInMonth = GetDatesOfSundays(System.DateTime.Now);  
  //var reportData = _reportingService.GetTradeUKKPISearches();
  //return View(reportData);
}

问题在于我的 allSundaysInMonth 类型字符串,当然也是空的。sReturn 是字符串类型,但我又传递了一个日期(我知道:))但是 allSundaysInMonth 应该是什么类型?sReturn 确实有正确的日期...我需要在控制器视图的下拉列表中显示这些日期,以便用户可以选择他/她需要为其运行报告的任何星期天。

谢谢

4

2 回答 2

5

怎么样

private IEnumerable<DateTime> GetDatesOfSundays(DateTime DatMonth)
{
    int iDayOffset = DatMonth.Day - 1;   
    DatMonth = DatMonth.AddDays(System.Convert.ToDouble(-DatMonth.Day + 1));
    DateTime DatMonth2 =
        DatMonth.AddMonths(1).AddDays(System.Convert.ToDouble(-1));
    while (DatMonth < DatMonth2)
    {
        if (DatMonth.DayOfWeek == System.DayOfWeek.Sunday)
        {
            yield return DatMonth;
        }

        DatMonth = DatMonth.AddDays(1.0);
    }
}

我很想将您的函数重写为类似这样的扩展

public static IEnumerable<Datetime> DaysOfMonth(
    this DateTime any,
    DayOfWeek day)
{
    // start at first of month
    var candidate = new DateTime(any.Year, any.Month, 1);

    var offset = (int)day - (int)candidate.DayOfWeek;

    if (offset < 0)
    {
        offset += 7
    }

    candidate = candidate.AddDays(offset);

    while (cadidate.Month == any.Month)
    {
        yield return candidate;
        candidate = candidate.AddDays(7.0)
    }
}

然后你可以像这样使用它

var allSundaysInMonth = DateTime.Now.DaysOfMonth(DayOfWeek.Sunday);

如果你想将 an 转换IEnumerable<DateTime>为 astring你可以这样做,

var listOfDates = string.Join<DateTime>(", ", allSundaysInMonth);

使用这个 string.Join重载


如果你真的想要这样的日子,DateTime[]你可以这样做(但没有必要)

DateTime[] allSundaysInMonth = GetDatesOfSundays(DateTime.Now).ToArray();

或者对于我的扩展示例

var allSundaysInMonth = DateTime.Now.DaysOfMonth(DayOfWeek.Sunday).ToArray();
于 2012-08-03T09:47:48.917 回答
0

您可以选择DateTime[]IEnumerable<DateTime>

您的方法签名应该是

private IEnumerable<DateTime> GetDatesOfSundays(DateTime DatMonth)

或者

private DateTime[] GetDatesOfSundays(DateTime DatMonth)

如果你还没有IEnumerable为此工作过

private DateTime[] GetDatesOfSundays(DateTime DatMonth)
{
    List<DateTime> lst = new List<DateTime>();

    DatMonth = DatMonth.AddDays(-DatMonth.Day + 1);
    DateTime DatMonth2 = DatMonth.AddMonths(1).AddDays(System.Convert.ToDouble(-1));

    while (DatMonth < DatMonth2)
    {
        if (DatMonth.DayOfWeek == System.DayOfWeek.Sunday)
        {
            lst.Add(DatMonth);
            DatMonth = DatMonth.AddDays(7);
            continue;
        }

        DatMonth = DatMonth.AddDays(1);
    }

    return lst.ToArray();
}

并将其称为

DateTime[] allSundaysInMonth = GetDatesOfSundays(System.DateTime.Now);
于 2012-08-03T09:45:39.520 回答