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" ></ASP:LABEL>:
</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> -
<asp:textbox id= "txtenddate" runat= "Server" ></asp:TextBox>
</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