BosonZhang's Blog



    不积跬步,无以至千里;不积小流,无以成江海。
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

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

Posted on 2008-10-22 22:45  qdzhbsh  阅读(977)  评论(4)    收藏  举报

     求一年中所有周六和周日的日期,大体的算法是遍历从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;
    }