获得以下内容的最佳方式是什么:
从今天的日期开始,返回一个可枚举的以下内容:
7月1日7月15日8月1日8月15日9月1日9月15日10月1日10月1日
应该考虑到如果它是年底的事情,那么它将在12月15日1月1日.
您的标题要求输入字符串,但问题文本要求输入可枚举字符串.这是什么?
无论如何,这是可枚举的:
public IEnumerable<DateTime> GetPaymentDates()
{
DateTime first = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1);
DateTime fifteenth = first.AddDays(14);
for (int i=0;i<4;i++)
{
yield return first;
yield return fifteenth;
first = first.AddMonths(1);
fifteenth = first.AddDays(14);
}
}
Run Code Online (Sandbox Code Playgroud)
或返回字符串的版本:
public IEnumerable<string> GetPaymentDates()
{
DateTime current = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1);
for (int i=0;i<4;i++)
{
yield return current.ToString("MMMM 1st");
yield return current.ToString("MMMM 15th");
current = current.AddMonths(1);
}
}
Run Code Online (Sandbox Code Playgroud)