[原创]自动获取当前日期下一周的年月日代码

自动获取当前日期的下一周或下一月

前台aspx文件
<%@ Page language="c#" Codebehind="WebForm4.aspx.cs" AutoEventWireup="false" Inherits="message1.WebForm4" %>


 
  WebForm4
  
  
  
  
 
 
  


       runat="server" AutoPostBack="True" Font-Size="9pt">
       Font-Size="9pt" Width="16px">周
       Font-Size="9pt" Width="16px">月    runat="server" Font-Size="9pt">    runat="server" Font-Size="9pt">
       Font-Size="9pt" Width="16px">年
       Font-Size="9pt">填报计划(下一年,月,周)

 


后台cs文件
#region 声明
//----------------------------------------------------------------------
//
// 作者: 李淼(Nick.Lee)
//
// 自动获取当前日期下一周的年月日代码
// 时间:2005-2-17
// [email protected]
// QQ:16503096
//
//----------------------------------------------------------------------
#endregion
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace message1
{
 ///


 /// WebForm4 的摘要说明。
 ///

 public class WebForm4 : System.Web.UI.Page
 {
  protected System.Web.UI.WebControls.DropDownList ddl_Year;
  protected System.Web.UI.WebControls.Calendar Calendar1;
  protected System.Web.UI.WebControls.DropDownList DropDownList2;
  protected System.Web.UI.WebControls.Label Label3;
  protected System.Web.UI.WebControls.Label Label2;
  protected System.Web.UI.WebControls.Label Label1;
  protected System.Web.UI.WebControls.Label Label4;
  protected System.Web.UI.WebControls.DropDownList DropDownList1;
  
 
  private void Page_Load(object sender, System.EventArgs e)
  {
   if(IsPostBack==false)
   {
    int ii=System.DateTime.Now.Year;
    this.ddl_Year.Items.Add(ii.ToString());
    ii=ii+1;
    this.ddl_Year.Items.Add(ii.ToString());

    for(int i=1;i<=12;i++)
    {
     System.Web.UI.WebControls.ListItem item=new ListItem();
     item.Text=i.ToString();
     this.DropDownList1.Items.Add(item);
    }

    if(System.DateTime.Now.Month.ToString()=="12")
    {
     DropDownList1.Items[0].Selected=true;
     ddl_Year.Items[1].Selected=true;

    }
    else
    {
     DropDownList1.Items[System.DateTime.Now.Month].Selected=true;
    }
    


    System.DateTime time1=new DateTime();
    time1=System.Convert.ToDateTime(System.DateTime.Now.Year+"-01-01");
    int weekNum=0;
    switch(time1.DayOfWeek.ToString())
    {
       
     case"Sunday":
      weekNum=6;
      break;
     case"Monday":
      weekNum=7;
      break;
     case"Tuesday":
      weekNum=8;
      break;
     case"Wednesday":
      weekNum=9;
      break;
     case"Thursday":
      weekNum=10;
      break;
     case"Friday":
      weekNum=11;
      break;
     case"Saturday":
      weekNum=12;
      break;
    }
    System.DateTime time2=new DateTime();
    System.TimeSpan Span1=System.DateTime.Now.Date-time1.Date;
    int day1=System.Convert.ToInt32(Span1.Days);
    day1=day1+weekNum+1;
       int weeks=System.Convert.ToInt32(day1/7);
       for(int i=1;i<=52;i++)
    {
     System.Web.UI.WebControls.ListItem item1=new ListItem();
     item1.Text=i.ToString();
     this.DropDownList2.Items.Add(item1);
    }

    if(weeks.ToString()=="53")
    {
     DropDownList2.Items[1].Selected=true;
     ddl_Year.Items[1].Selected=true;

    }
    else if(weeks.ToString()=="52")
    {
     ddl_Year.Items[1].Selected=true;
     DropDownList2.Items[0].Selected=true;
    }
    else
    {
     DropDownList2.Items[weeks].Selected=true;
    }

 


    
      }
       // 在此处放置用户代码以初始化页面
  }

  #region Web 窗体设计器生成的代码
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  ///


  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  ///

  private void InitializeComponent()
  {   
   this.DropDownList1.SelectedIndexChanged += new System.EventHandler(this.DropDownList1_SelectedIndexChanged);
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion

  private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
  {
   this.Response.Write(this.DropDownList1.SelectedItem.Text);
  }
 }
}

你可能感兴趣的:(asp.net)