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

类别:.NET开发 点击:0 评论:0 推荐:

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

前台aspx文件
<%@ Page language="c#" Codebehind="WebForm4.aspx.cs" AutoEventWireup="false" Inherits="message1.WebForm4" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
 <HEAD>
  <title>WebForm4</title>
  <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
  <meta content="C#" name="CODE_LANGUAGE">
  <meta content="JavaScript" name="vs_defaultClientScript">
  <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
 </HEAD>
 <body MS_POSITIONING="GridLayout">
  <form id="Form1" method="post" runat="server">
   <asp:dropdownlist id="DropDownList1" style="Z-INDEX: 101; LEFT: 304px; POSITION: absolute; TOP: 104px"
    runat="server" AutoPostBack="True" Font-Size="9pt"></asp:dropdownlist>
   <asp:Label id="Label3" style="Z-INDEX: 106; LEFT: 496px; POSITION: absolute; TOP: 111px" runat="server"
    Font-Size="9pt" Width="16px">周</asp:Label>
   <asp:Label id="Label2" style="Z-INDEX: 105; LEFT: 376px; POSITION: absolute; TOP: 111px" runat="server"
    Font-Size="9pt" Width="16px">月</asp:Label><asp:dropdownlist id="ddl_Year" style="Z-INDEX: 102; LEFT: 192px; POSITION: absolute; TOP: 104px"
    runat="server" Font-Size="9pt"></asp:dropdownlist><asp:dropdownlist id="DropDownList2" style="Z-INDEX: 103; LEFT: 424px; POSITION: absolute; TOP: 104px"
    runat="server" Font-Size="9pt"></asp:dropdownlist>
   <asp:Label id="Label1" style="Z-INDEX: 104; LEFT: 264px; POSITION: absolute; TOP: 111px" runat="server"
    Font-Size="9pt" Width="16px">年</asp:Label>
   <asp:Label id="Label4" style="Z-INDEX: 107; LEFT: 48px; POSITION: absolute; TOP: 109px" runat="server"
    Font-Size="9pt">填报计划(下一年,月,周)</asp:Label></form>
 </body>
</HTML>

后台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
{
 /// <summary>
 /// WebForm4 的摘要说明。
 /// </summary>
 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);
  }
  
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  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);
  }
 }
}

本文地址:http://com.8s8s.com/it/it41778.htm