求一年中所有周六和周日的日期,大体的算法是遍历从1月1日到12月31日的每一天,然后判断每一天是否为周六或周日。如何遍历呢?其实说来也很简单,DateTime类型是支持for循环的。 方法实现如下:
public DateTime[] GetAllGeneralHolidaysByYear(int year)
{
DateTime[] ret = null;
string temp = "Saturday;Sunday";
DateTime startDate = new DateTime(year, 1, 1);
DateTime endDate = new DateTime(year, 12, 31);
ArrayList al = new ArrayList();
for (DateTime dt = startDate; dt <= endDate; dt = dt.AddDays(1))
{
if (temp.Contains(dt.DayOfWeek.ToString()))
{
al.Add(dt);
}
}
if (al.Count > 0)
{
ret = new DateTime[al.Count];
for (int i = 0; i < al.Count; i++)
{
ret[i] = (DateTime)al[i];
}
}
return ret;
}
浙公网安备 33010602011771号