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

时间: 2024-08-29 12:16:05

ASP.NET中获取当日,当周,当月,当年的日期的相关文章

再论 ASP.NET 中获取客户端IP地址

说到IP获取无非是我们常见的以下几种方式,但是具体获取的值具体区别在哪?网上不乏相关文章,说的也是很详细,但是真正使用起来,还有很多不太对的地方.IP在不同系统中,应用相当广泛,常见的日志记录.广告分区域投放等. 1: HttpContext.Current.Request.ServerVariables["HTTP_VIA"]; 2: HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"

如何在asp.net中获取GridView隐藏列的值?

在阅读本文之前,我获取gridview某行某列的值一般做法是这样的:row.Cells[3].Text.ToString().有点傻瓜呵呵 在Asp.net 2.0中增加了一个新的数据绑定控件:GridView,其目的用来取代Asp.net1.x中的DataGrid控件.获取GridView中的某列值的方法为   protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)    {        stri

asp.net中获取当前url的方法

HttpContext.Current.Request.Url.ToString() 并不可靠. 如果当前URL为 http://localhost/search.aspx?user=http://csharp.xdowns.com&tag=%BC%BC%CA%F5 通过HttpContext.Current.Request.Url.ToString()获取到的却是 http://localhost/search.aspxuser=http://csharp.xdowns.com&tag=

ASP.NET中获取登录用户ID 用户身份验证(转载)

如果VS2005及VS2008中使用菜单[网站]-[ASP.NET配置]进入使用网站管理工具(WAT)的表单验证(Forms验证)方式来建立网站数据库,默认数据库名为ASPNETDB.MDF.系统将会自动建立一套系统数据表(VS2008中为11个这样的数据表),利用这个系统数据表可以快捷地进行创建新用户.角色.用户登录.修改用户口令等开发. 在后续开发过程中,需要反复使用用户ID.判断用户是否为登录用户或者是否为某一角色,这些常用操作如下: 1.获取当前登录用户ID Membership.Get

sqlite 获取当天当周当月等数据

----昨天 select * from 表 where Time>=datetime('now','start of day','-1 day') and Time<datetime('now','start of day','+0 day') ----当天 select * from 表 where Time>=datetime('now','start of day','+0 day') and Time<datetime('now','start of day','+1 d

asp.net中获取Layer弹出层返回值

1.MainPage.aspx中点击按钮利用Layer弹出层,代码如下: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MainPage.aspx.cs" Inherits="demo.MainPage" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xh

asp.net 中如何获取CheckBoxList的值

asp.net 中如何获取CheckBoxList的值 string item = string.Empty; if (CheckBoxList1.SelectedIndex == -1) { Alert("请选择"); return; } else { foreach (ListItem li in CheckBoxList1.Items) { if (li.Selected) { item += li.Value.ToString() + ","; } } Se

ASP.net中DateTime获取当前系统时间的大全

在c# / ASP.net中我们可以通过使用DataTime这个类来获取当前的时间.通过调用类中的各种方法我们可以获取不同的时间:如:日期(2008-09-04).时间(12:12:12).日期+时间(2008-09-04 12:11:10)等. //获取日期+时间   DateTime.Now.ToString();                    // 2008-9-4 20:02:10   DateTime.Now.ToLocalTime().ToString();         

如何在ASP.NET MVC 中获取当前URL、controller、action

一.URL的获取很简单,ASP.NET通用: [1]获取 完整url (协议名+域名+虚拟目录名+文件名+参数) string url=Request.Url.ToString(); [2]获取 虚拟目录名+页面名+参数: string url=Request.RawUrl;(或 string url=Request.Url.PathAndQuery;) [3]获取 虚拟目录名+页面名:string url=HttpContext.Current.Request.Url.AbsolutePath