随便一个年份,如2009年,如何将2009中,所有周末列出来?2009-1-3 六
2009-1-4 日
.......
2009-12-5 六
2009-12-6 日有人做过吗?C#版或javascript版的都可以。。
谢谢。。

解决方案 »

  1.   


            static void Main(string[] args)
            {
                GetDate(2009);
            }        static void GetDate(int year)
            {
                for (DateTime dt = new DateTime(year, 1, 1); dt <= new DateTime(year, 12, 31); dt = dt.AddDays(1))
                    if (dt.DayOfWeek == DayOfWeek.Saturday || dt.DayOfWeek == DayOfWeek.Sunday)
                        Console.WriteLine(dt.ToString("yyyy-MM-dd") + " " + (dt.DayOfWeek == DayOfWeek.Saturday ? "六" : "日"));
            }
      

  2.   

    改成这样能更快点:        static void GetDate(int year)
            {
                for (DateTime dt = new DateTime(year, 1, 1); dt <= new DateTime(year, 12, 31); )
                {
                    if (dt.DayOfWeek == DayOfWeek.Saturday)
                        Console.WriteLine(dt.ToString("yyyy-MM-dd") + " 六");
                    else if (dt.DayOfWeek == DayOfWeek.Sunday)
                    {
                        Console.WriteLine(dt.ToString("yyyy-MM-dd") + " 日");
                        dt = dt.AddDays(6);
                        continue;
                    }
                    dt = dt.AddDays(1);
                }
            }
      

  3.   

    declare @y int
    set @y=2009declare @sdate datetime
    declare @edate datetime
    set @sdate=convert(datetime,'01/01/'+cast(@y as varchar(4)),101)
    set @edate=convert(datetime,'12/31/'+cast(@y as varchar(4)),101)while(@sdate<@edate)
    begin
    set @sdate=dateadd(d,1,@sdate)
    if datepart(weekday,@sdate)=1 or datepart(weekday,@sdate)=7
    begin 
    print convert(varchar(10),@sdate,101)+'--'+datename(weekday,@sdate)
    end 
    end
    ------------------------------------------------------------01/03/2009--星期六
    01/04/2009--星期日
    01/10/2009--星期六
    01/11/2009--星期日
    01/17/2009--星期六
    01/18/2009--星期日
    01/24/2009--星期六
    01/25/2009--星期日
    01/31/2009--星期六
    02/01/2009--星期日
    02/07/2009--星期六
    02/08/2009--星期日
    02/14/2009--星期六
    02/15/2009--星期日
    02/21/2009--星期六
    02/22/2009--星期日
    02/28/2009--星期六
    03/01/2009--星期日
    03/07/2009--星期六
    03/08/2009--星期日
    03/14/2009--星期六
    03/15/2009--星期日
    03/21/2009--星期六
    03/22/2009--星期日
    03/28/2009--星期六
    03/29/2009--星期日
    04/04/2009--星期六
    04/05/2009--星期日
    04/11/2009--星期六
    04/12/2009--星期日
    04/18/2009--星期六
    04/19/2009--星期日
    04/25/2009--星期六
    04/26/2009--星期日
    05/02/2009--星期六
    05/03/2009--星期日
    05/09/2009--星期六
    05/10/2009--星期日
    05/16/2009--星期六
    05/17/2009--星期日
    05/23/2009--星期六
    05/24/2009--星期日
    05/30/2009--星期六
    05/31/2009--星期日
    06/06/2009--星期六
    06/07/2009--星期日
    06/13/2009--星期六
    06/14/2009--星期日
    06/20/2009--星期六
    06/21/2009--星期日
    06/27/2009--星期六
    06/28/2009--星期日
    07/04/2009--星期六
    07/05/2009--星期日
    07/11/2009--星期六
    07/12/2009--星期日
    07/18/2009--星期六
    07/19/2009--星期日
    07/25/2009--星期六
    07/26/2009--星期日
    08/01/2009--星期六
    08/02/2009--星期日
    08/08/2009--星期六
    08/09/2009--星期日
    08/15/2009--星期六
    08/16/2009--星期日
    08/22/2009--星期六
    08/23/2009--星期日
    08/29/2009--星期六
    08/30/2009--星期日
    09/05/2009--星期六
    09/06/2009--星期日
    09/12/2009--星期六
    09/13/2009--星期日
    09/19/2009--星期六
    09/20/2009--星期日
    09/26/2009--星期六
    09/27/2009--星期日
    10/03/2009--星期六
    10/04/2009--星期日
    10/10/2009--星期六
    10/11/2009--星期日
    10/17/2009--星期六
    10/18/2009--星期日
    10/24/2009--星期六
    10/25/2009--星期日
    10/31/2009--星期六
    11/01/2009--星期日
    11/07/2009--星期六
    11/08/2009--星期日
    11/14/2009--星期六
    11/15/2009--星期日
    11/21/2009--星期六
    11/22/2009--星期日
    11/28/2009--星期六
    11/29/2009--星期日
    12/05/2009--星期六
    12/06/2009--星期日
    12/12/2009--星期六
    12/13/2009--星期日
    12/19/2009--星期六
    12/20/2009--星期日
    12/26/2009--星期六
    12/27/2009--星期日
      

  4.   

    declare @y int
    set @y=2004declare @sdate datetime
    declare @edate datetime
    set @sdate=convert(datetime,'01/01/'+cast(@y as varchar(4)),101)
    set @edate=convert(datetime,'12/31/'+cast(@y as varchar(4)),101)while(@sdate<@edate)
    begin
    set @sdate=dateadd(d,1,@sdate)
    if datepart(weekday,@sdate)=1 or datepart(weekday,@sdate)=7
    begin 
    print convert(varchar(10),@sdate,101)+'--'+datename(weekday,@sdate)
    end 
    end------------------------------------------------01/03/2004--星期六
    01/04/2004--星期日
    01/10/2004--星期六
    01/11/2004--星期日
    01/17/2004--星期六
    01/18/2004--星期日
    01/24/2004--星期六
    01/25/2004--星期日
    01/31/2004--星期六
    02/01/2004--星期日
    02/07/2004--星期六
    02/08/2004--星期日
    02/14/2004--星期六
    02/15/2004--星期日
    02/21/2004--星期六
    02/22/2004--星期日
    02/28/2004--星期六
    02/29/2004--星期日
    03/06/2004--星期六
    03/07/2004--星期日
    03/13/2004--星期六
    03/14/2004--星期日
    03/20/2004--星期六
    03/21/2004--星期日
    03/27/2004--星期六
    03/28/2004--星期日
    04/03/2004--星期六
    04/04/2004--星期日
    04/10/2004--星期六
    04/11/2004--星期日
    04/17/2004--星期六
    04/18/2004--星期日
    04/24/2004--星期六
    04/25/2004--星期日
    05/01/2004--星期六
    05/02/2004--星期日
    05/08/2004--星期六
    05/09/2004--星期日
    05/15/2004--星期六
    05/16/2004--星期日
    05/22/2004--星期六
    05/23/2004--星期日
    05/29/2004--星期六
    05/30/2004--星期日
    06/05/2004--星期六
    06/06/2004--星期日
    06/12/2004--星期六
    06/13/2004--星期日
    06/19/2004--星期六
    06/20/2004--星期日
    06/26/2004--星期六
    06/27/2004--星期日
    07/03/2004--星期六
    07/04/2004--星期日
    07/10/2004--星期六
    07/11/2004--星期日
    07/17/2004--星期六
    07/18/2004--星期日
    07/24/2004--星期六
    07/25/2004--星期日
    07/31/2004--星期六
    08/01/2004--星期日
    08/07/2004--星期六
    08/08/2004--星期日
    08/14/2004--星期六
    08/15/2004--星期日
    08/21/2004--星期六
    08/22/2004--星期日
    08/28/2004--星期六
    08/29/2004--星期日
    09/04/2004--星期六
    09/05/2004--星期日
    09/11/2004--星期六
    09/12/2004--星期日
    09/18/2004--星期六
    09/19/2004--星期日
    09/25/2004--星期六
    09/26/2004--星期日
    10/02/2004--星期六
    10/03/2004--星期日
    10/09/2004--星期六
    10/10/2004--星期日
    10/16/2004--星期六
    10/17/2004--星期日
    10/23/2004--星期六
    10/24/2004--星期日
    10/30/2004--星期六
    10/31/2004--星期日
    11/06/2004--星期六
    11/07/2004--星期日
    11/13/2004--星期六
    11/14/2004--星期日
    11/20/2004--星期六
    11/21/2004--星期日
    11/27/2004--星期六
    11/28/2004--星期日
    12/04/2004--星期六
    12/05/2004--星期日
    12/11/2004--星期六
    12/12/2004--星期日
    12/18/2004--星期六
    12/19/2004--星期日
    12/25/2004--星期六
    12/26/2004--星期日