Can anybody tell me that how I can get current month and year and show it in a label in ASP.NET?
import datetime; today = str(datetime. date. today()); curr_year = int(today[:4]); curr_month = int(today[5:7]); This will get you the current month and year in integer format.
If you have following two labels:
<asp:Label ID="MonthLabel" runat="server" />
<asp:Label ID="YearLabel" runat="server" />
Than you can use following code just need to set the Text Property for these labels like:
MonthLabel.Text = DateTime.Now.Month.ToString();
YearLabel.Text = DateTime.Now.Year.ToString();
Use the DateTime.Now property. This returns a DateTime object that contains a Year and Month property (both are integers).
string currentMonth = DateTime.Now.Month.ToString();
string currentYear = DateTime.Now.Year.ToString();
monthLabel.Text = currentMonth;
yearLabel.Text = currentYear;
Like this:
DateTime.Now.ToString("MMMM yyyy")
For more information, see DateTime Format Strings.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With