Asp. NET gets the day, when the week, the month, the date of the current year

Source: Internet
Author: User

Asp. NET gets the day, when the week, the month, the date of the current year


in ASP. NET development, often encounter to get the day, when the week, the month, the date of the year.
The following will be the source code to share with you.

The code in ASPX is as follows:

<tablecellspacing= "2" cellpadding= "0" width= "98%" align= "Center" >
<tr>
<td>
<asp:label id= "lbldate" runat= "Server" text= "date" &GT;&LT;/ASP:LABEL&GT;:
</td>
<td>
<asp:button id= "btnyear" runat= "Server" text= "year" cssclass= "Buttoncss" onclick= "Btnyear_click"/>
<asp:button id= "Btnmonth" runat= "server" text= "month" cssclass= "Buttoncss" onclick= "Btnmonth_click"/>
<asp:button id= "Btnweek" runat= "Server" text= "When Week" cssclass= "Buttoncss" onclick= "Btnweek_click"/>
<asp:button id= "Btnday" runat= "Server" text= "Day" 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 code is

protected Voidbtnyear_click (object sender, EventArgs e)
{
Stringtoyear = DateTime.Now.ToString ("yyyy");
Txtstartdate.text = Toyear + "-01-01";
Txtenddate.text = Toyear + "-12-31";
}

protected Voidbtnmonth_click (object sender, EventArgs e)
{
Datetimetoday = DateTime.Now;
Stringlastday = Datetime.daysinmonth (today. Year,today. Month). ToString ();
Txtstartdate.text = DateTime.Now.ToString ("yyyy-mm") + "-01";
Txtenddate.text = today. ToString ("yyyy-mm") + "-" + lastday;
}

protected Voidbtnweek_click (object sender, EventArgs e)
{
Datetimetoday = DateTime.Now;
Int week =convert.toint32 (today. DayOfWeek);
Datetimesunday = today. AddDays (-week);
Datetimesaturday = today. AddDays (7-week);
Txtstartdate.text = Sunday. ToString ("Yyyy-mm-dd");
Txtenddate.text = Saturday. ToString ("Yyyy-mm-dd");
}

protected Voidbtnday_click (object sender, EventArgs e)
{
String today= DateTime.Now.ToString ("Yyyy-mm-dd");
Txtstartdate.text = Today;
Txtenddate.text = Today;
}

Vb. NET code is:

Protected Subbtnyear_click (ByVal sender as Object, ByVal e as System.EventArgs) Handles Btnyear.click
Dim Toyearas String = DateTime.Now.ToString ("yyyy")
Txtstartdate.text = Toyear + "-01-01"
Txtenddate.text = Toyear + "-12-31"
End Sub

Protected Subbtnmonth_click (ByVal sender as Object, ByVal e as System.EventArgs) Handles Btnmonth.click
Dim Today Asdatetime = DateTime.Now
Dim Lastdayas 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 Subbtnweek_click (ByVal sender as Object, ByVal e as System.EventArgs) Handles Btnweek.click
Dim Today Asdatetime = DateTime.Now
Dim Week AsInt32 = Convert.ToInt32 (today. DayOfWeek)
Dim Sundayas DateTime = today. AddDays (-week)
Dim Saturdayas DateTime = today. AddDays (7-week)
Txtstartdate.text = Sunday. ToString ("Yyyy-mm-dd")
Txtenddate.text = Saturday. ToString ("Yyyy-mm-dd")
End Sub

Protected Subbtnday_click (ByVal sender as Object, ByVal e as System.EventArgs) Handles Btnday.click
Dim Today asstring = DateTime.Now.ToString ("Yyyy-mm-dd")
Txtstartdate.text = Today
Txtenddate.text = Today
End Sub

Asp. NET gets the day, when the week, the month, the date of the current year

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.