我想弄清楚如何检索多天的事件。现在我们将日计算作为枚举推送到这里
internal static class DayCalc
{
public static DateTime GetDateTime(Enums.Day day = Enums.Day.Today)
{
DateTime retVal = DateTime.Now;
if (day == Enums.Day.Tomorrow)
{
retVal = retVal.AddDays(1);
retVal = SetTime(retVal);
}
else if (day == Enums.Day.DayAfterTomorrow)
{
retVal = retVal.AddDays(2);
retVal = SetTime(retVal);
}
else if (day != Enums.Day.Today)
{
// -1, -2, -3 are handeled already so we can use enum Day as enum DayOfTheWeek now
retVal = GetNextWeekday((DayOfWeek)Enum.Parse(typeof(DayOfWeek), day.ToString()));
}
return retVal;
我的问题是我希望能够获得多日参赛作品,这可能吗?
答案 0 :(得分:0)
如果您只需要返回的日期/时间,您可以添加一个额外的方法来调用您现有的方法:
public static IEnumerable<DateTime> GetDateTimesOnly(IEnumerable<Enums.Day> days)
{
foreach (var day in days)
yield return GetDateTime(day);
}
如果您需要引用Day枚举,可以使用字典(前提是输入值是唯一的):
public static IDictionary<Enums.Day, DateTime> GetDaysAndDateTimes(IEnumerable<Enums.Day> days)
{
var result = new Dictionary<Enums.Day, DateTime>();
days.ToList().ForEach(d => result.Add(d, GetDateTime(d)));
return result;
}
<强>更新强>
由于您已经有一段时间的枚举,您可以使用句点创建重载:
public static DateTime GetDateTime(Enums.DateTimePeriod period = Enums.DateTimePeriod.Week)
{
DateTime retVal = DateTime.Now;
if (period == Enums.DateTimePeriod.Day)
{
retVal = retVal.AddDays(1);
retVal = SetTime(retVal);
}
else if (period == Enums.DateTimePeriod.Week)
{
retVal = retVal.AddDays(7);
retVal = SetTime(retVal);
}
return retVal;
}