您的位置:首页 > 其它

求一年中所有周六和周日的日期的方法

2008-10-22 22:45 267 查看
求一年中所有周六和周日的日期,大体的算法是遍历从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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: