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

推荐订阅源

The Hacker News
The Hacker News
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
P
Privacy & Cybersecurity Law Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Simon Willison's Weblog
Simon Willison's Weblog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
TaoSecurity Blog
TaoSecurity Blog
C
CERT Recently Published Vulnerability Notes
S
Security @ Cisco Blogs
Google DeepMind News
Google DeepMind News
Security Archives - TechRepublic
Security Archives - TechRepublic
T
Tenable Blog
O
OpenAI News
NISL@THU
NISL@THU
AI
AI
T
Threat Research - Cisco Blogs
Know Your Adversary
Know Your Adversary
V
Vulnerabilities – Threatpost
Schneier on Security
Schneier on Security
W
WeLiveSecurity
P
Privacy International News Feed
Cisco Talos Blog
Cisco Talos Blog
S
Secure Thoughts
D
Docker
博客园 - 三生石上(FineUI控件)
T
Troy Hunt's Blog
T
Threatpost
C
Cyber Attacks, Cyber Crime and Cyber Security
Vercel News
Vercel News
H
Hacker News: Front Page
B
Blog
S
SegmentFault 最新的问题
H
Heimdal Security Blog
T
The Blog of Author Tim Ferriss
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
G
GRAHAM CLULEY
AWS News Blog
AWS News Blog
N
News and Events Feed by Topic
L
LINUX DO - 热门话题
小众软件
小众软件
爱范儿
爱范儿
T
The Exploit Database - CXSecurity.com
Hacker News: Ask HN
Hacker News: Ask HN
Recent Announcements
Recent Announcements
Recent Commits to openclaw:main
Recent Commits to openclaw:main
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
PCI Perspectives
PCI Perspectives
I
Intezer
云风的 BLOG
云风的 BLOG
P
Proofpoint News Feed

博客园 - 飞花雪月

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