惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
SegmentFault 最新的问题
爱范儿
爱范儿
博客园 - 三生石上(FineUI控件)
Microsoft Security Blog
Microsoft Security Blog
Google DeepMind News
Google DeepMind News
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
GbyAI
GbyAI
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
V
V2EX
酷 壳 – CoolShell
酷 壳 – CoolShell
量子位
博客园_首页
T
Tailwind CSS Blog
aimingoo的专栏
aimingoo的专栏
A
About on SuperTechFans
T
The Blog of Author Tim Ferriss
Stack Overflow Blog
Stack Overflow Blog
Recent Announcements
Recent Announcements
P
Proofpoint News Feed
博客园 - 司徒正美
有赞技术团队
有赞技术团队
Engineering at Meta
Engineering at Meta
Last Week in AI
Last Week in AI
MongoDB | Blog
MongoDB | Blog

博客园 - 飞花雪月

ASP.NET Core-wwwroot文件夹 ASP.NET MVC4中的bundles特性引发服务器拒绝访问(403错误) 两步验证Authy时间同步问题 您的项目引用了最新实体框架;但是,找不到数据链接所需的与版本兼容的实体框架数据库 EF6使用Mysql的技巧 教你如何调用百度编辑器ueditor的上传图片、上传文件等模块 Easyui Tree方法扩展 - getLevel(获取节点级别) HTML5添加 video 视频标签后仍然无法播放的解决方法 IIS添加MIEI类型 ASP.NET性能优化之减少请求 Convert.ToInt32()与int.Parse()的区别 C#/JS 获取二维数组组合 【转】浅析Sql Server参数化查询 【转】Sql Server参数化查询之where in和like实现之xml和DataTable传参 【转】Sql Server参数化查询之where in和like实现详解 推薦使用 Microsoft Anti-Cross Site Scripting Library V3.0 推薦使用 Microsoft Anti-Cross Site Scripting Library v3.1 IIS中ASP.NET安全配置 好用的SQLSERVER数据库自动备份工具SQLBackupAndFTP(功能全面) iframe在ipad safari的显示 从WEB SERVICE 上返回大数据量的DATASET
ASP.NET中获取当日,当周,当月,当年的日期
飞花雪月 · 2015-01-29 · via 博客园 - 飞花雪月

 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