Can anybody tell me that how I can get current month and year and show it in a label in ASP.NET?
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;
label1.Text = DateTime.Now.Month.ToString();
and
label2.Text = DateTime.Now.Year.ToString();
public string GetCurrentYear()
{
string CurrentYear = DateTime.Now.Year.ToString();
return CurrentYear;
}
public string GetCurrentMonth()
{
string CurrentMonth = DateTime.Now.Month.ToString();
return CurrentMonth;
}
Find Current Year Using Razor try this
@DateTime.Now.Year
Like this:
DateTime.Now.ToString("MMMM yyyy")
For more information, see DateTime Format Strings.
You can use this:
<p>© <%: DateTime.Now.Year %> - My ASP.NET Application</p>