Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get current month and year

Tags:

asp.net

Can anybody tell me that how I can get current month and year and show it in a label in ASP.NET?

like image 322
preety Avatar asked Oct 08 '10 13:10

preety


People also ask

How do I get the current month and year in Python?

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.


3 Answers

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();
like image 53
devil_coder Avatar answered Oct 24 '22 08:10

devil_coder


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 image 44
Edwin de Koning Avatar answered Oct 24 '22 08:10

Edwin de Koning


Like this:

DateTime.Now.ToString("MMMM yyyy")

For more information, see DateTime Format Strings.

like image 27
SLaks Avatar answered Oct 24 '22 10:10

SLaks