码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2015-01-29 12:20:59      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:

 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

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

标签:

原文地址:http://www.cnblogs.com/jsonzheng/p/4259163.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!