【发布时间】:2012-08-01 08:27:11
【问题描述】:
我想提取当月的所有星期天,并有这个代码:
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 确实有正确的日期...我需要在控制器视图的下拉列表中显示这些日期,以便用户可以选择他/她需要为其运行报告的任何星期日。
谢谢
【问题讨论】:
-
IEnumerable<DateTime>我觉得不错 -
我试过了: IEnumerable
allSundaysInMonth = GetDatesOfSundays(System.DateTime.Now);它给出了错误无法将类型'string'隐式转换为'System.Collections.Generic.IEnumerable ':( -
@charlie,你可以将你的变量声明为
string,将方法的返回类型声明为IEnumerable<DateTime>,但不要指望这两者可以结合在一起。一个是日期序列,另一个是一堆字符。根本不是相同的类型,因此您不能将一种分配给另一种。
标签: c#