首页 > 代码库 > ASP.NET中获取当日,当周,当月,当年的日期

ASP.NET中获取当日,当周,当月,当年的日期

 ASP.NET中获取当日,当周,当月,当年的日期


在ASP.NET开发中,经常会碰到要获取当日,当周,当月,当年的日期。
以下将源码贴出来和大家分享。

 技术分享

aspx中代码如下:

 

<tablecellspacing="2" cellpadding="0" width="98%"align="center">
   <tr>
       <td>
           <asp:Label ID="lblDate" runat="server"Text="日期"></asp:Label>:
       </td>
       <td>
           <asp:Button ID="btnYear" runat="server" Text="当年"CssClass="ButtonCss" OnClick="btnYear_Click"/>
           <asp:Button ID="btnMonth" runat="server" Text="当月"CssClass="ButtonCss" OnClick="btnMonth_Click"/>
           <asp:Button ID="btnWeek" runat="server" Text="当周"CssClass="ButtonCss" OnClick="btnWeek_Click"/>
           <asp:Button ID="btnDay" runat="server" Text="当天"CssClass="ButtonCss" OnClick="btnDay_Click" />
       </td>
   </tr>
   <tr>
       <td>
       </td>
       <td>
           <asp:TextBox ID="txtStartDate"runat="server"></asp:TextBox>&nbsp;-&nbsp;
           <asp:TextBox ID="txtEndDate"runat="server"></asp:TextBox>&nbsp;
       </td>
   </tr>
</table>
 
C#.NET代码为

 

protected voidbtnYear_Click(object sender, EventArgs e)
{
    stringtoyear = DateTime.Now.ToString("yyyy");
   txtStartDate.Text = toyear + "-01-01";
   txtEndDate.Text = toyear + "-12-31";
}

 

protected voidbtnMonth_Click(object sender, EventArgs e)
{
    DateTimetoday = DateTime.Now;
    stringlastday = DateTime.DaysInMonth(today.Year,today.Month).ToString();
   txtStartDate.Text = DateTime.Now.ToString("yyyy-MM") + "-01";
   txtEndDate.Text = today.ToString("yyyy-MM") + "-" + lastday;
}

 

protected voidbtnWeek_Click(object sender, EventArgs e)
{
    DateTimetoday = DateTime.Now;
    int week =Convert.ToInt32(today.DayOfWeek);
    DateTimesunday = today.AddDays(-week);
    DateTimesaturday = today.AddDays(7 - week);
   txtStartDate.Text = sunday.ToString("yyyy-MM-dd");
   txtEndDate.Text = saturday.ToString("yyyy-MM-dd");
}

 

protected voidbtnDay_Click(object sender, EventArgs e)
{
    string today= DateTime.Now.ToString("yyyy-MM-dd");
   txtStartDate.Text = today;
   txtEndDate.Text = today;
}

 

VB.NET代码为:

 

Protected SubbtnYear_Click(ByVal sender As Object, ByVal e As System.EventArgs)Handles btnYear.Click
    Dim toyearAs String = DateTime.Now.ToString("yyyy")
   txtStartDate.Text = toyear + "-01-01"
   txtEndDate.Text = toyear + "-12-31"
End Sub

 

Protected SubbtnMonth_Click(ByVal sender As Object, ByVal e As System.EventArgs)Handles btnMonth.Click
    Dim today AsDateTime = DateTime.Now
    Dim lastdayAs String = DateTime.DaysInMonth(today.Year,today.Month).ToString()
   txtStartDate.Text = DateTime.Now.ToString("yyyy-MM") + "-01"
   txtEndDate.Text = today.ToString("yyyy-MM") + "-" + lastday
End Sub

 

Protected SubbtnWeek_Click(ByVal sender As Object, ByVal e As System.EventArgs)Handles btnWeek.Click
    Dim today AsDateTime = DateTime.Now
    Dim week AsInt32 = Convert.ToInt32(today.DayOfWeek)
    Dim sundayAs DateTime = today.AddDays(-week)
    Dim saturdayAs DateTime = today.AddDays(7 - week)
   txtStartDate.Text = sunday.ToString("yyyy-MM-dd")
   txtEndDate.Text = saturday.ToString("yyyy-MM-dd")
End Sub

 

Protected SubbtnDay_Click(ByVal sender As Object, ByVal e As System.EventArgs)Handles btnDay.Click
    Dim today AsString = DateTime.Now.ToString("yyyy-MM-dd")
   txtStartDate.Text = today
   txtEndDate.Text = today
End Sub

ASP.NET中获取当日,当周,当月,当年的日期