c#获取当前年的周数及当前月的天数示例代码_C#教程

分享到:

获取当前年的周数

 /// <summary>
 /// 获得今年有几周
 /// </summary>
 /// <param name="year"></param>
 /// <returns></returns>
  public int GetWeekOfYear(int year)
  {
       DateTime the_Date = new DateTime(year,1,1);//本年的第一天
        TimeSpan tt=the_Date.AddYears(1)-the_Date;//求出本年有几天
        return tt.Days / 7 + 1; //因为年只有366天和365天除以7有余数所以始终需要加一周
  }

获取当前月的天数

第一种:

   DateTime dt = new DateTime(2007, 1, 1);

   //在.net中内部处理是减去+1后的月份得到当前月的天数(实际上这种方式是算出他们的差)例如:
   //当前月是2007年1月1日,通过AddMonths方法加一个月是2007年2月1日,在.net中将算出它们间隔多少天.
   //所以我们可以利用这种方法来求出当前月有几天
    TimeSpan tt = dt.AddMonths(1) - dt;
   // DateTime dt = new DateTime(2007,1,31); 如果时间是2007,1,31加一个月减当前月就不行,看下面解释:
   /*比如说给你做一个项目,2010-01-01给你做的,一个月做完,就是2010-02-01交付
    现在2010-01-31号给你做,也是要求一个月做完,本来是应该在2010-02-31号交付的,但是2月没有31号,所以交付的时间就变成2010-02-28号了
    AddMonths只是增加一个月的时间*/

    Response.Write(tt);

第二种:

public static int year;
    public static int month;
    public static int days;
    protected void Button1_Click(object sender, EventArgs e)
    {
        year = DateTime.Now.Year;
        month = DateTime.Now.Month;
        //得到当前月的天数
        switch (month)
        {
            case 1:
                days = 31;
                break;
            case 2:
                if (DateTime.IsLeapYear(year))
                {
                    //闰年二月为29天
                    days = 29;
                }
                else
                {
                    //不是闰年,二月为28天
                    days = 28;
                }
                break;
            case 3:
                days = 31;
                break;
            case 4:
                days = 30;
                break;
            case 5:
                days = 31;
                break;
            case 6:
                days = 30;
                break;
            case 7:
                days = 31;
                break;
            case 8:
                days = 31;
                break;
            case 9:
                days = 30;
                break;
            case 10:
                days = 31;
                break;
            case 11:
                days = 30;
                break;
            case 12:
                days = 31;
                break;
        }
        TextBox1.Text = days.ToString();
    }


protected void Page_Load(object sender, EventArgs e)
        {
            int day=GetWeekDay(2010,11);
            if (day != 0)
            {
                Response.Write(day);
            }
        }

        public int GetWeekDay(int year,int month)
        {
            switch (month)
            {
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    return 31;

                case 2:
                    if (DateTime.IsLeapYear(year))
                        return 29;
                    else
                        return 28;

                case 4:
                case 6:
                case 9:
                case 11:
                    return 30;
                default:
                    return 0;    
            }

昵    称:
验证码: