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

时间:2022-08-05 08:38:18

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

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

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

aspx中代码如下:

<table
cellspacing="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 void
btnYear_Click(object sender, EventArgs e)
{
    string
toyear = DateTime.Now.ToString("yyyy");
   
txtStartDate.Text = toyear + "-01-01";
   
txtEndDate.Text = toyear + "-12-31";
}

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

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

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

VB.NET代码为:

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

Protected Sub
btnMonth_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Handles btnMonth.Click
    Dim today As
DateTime = DateTime.Now
    Dim lastday
As 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 Sub
btnWeek_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Handles btnWeek.Click
    Dim today As
DateTime = DateTime.Now
    Dim week As
Int32 = Convert.ToInt32(today.DayOfWeek)
    Dim sunday
As DateTime = today.AddDays(-week)
    Dim saturday
As DateTime = today.AddDays(7 - week)
   
txtStartDate.Text = sunday.ToString("yyyy-MM-dd")
   
txtEndDate.Text = saturday.ToString("yyyy-MM-dd")
End Sub

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