how to get current month and year

asp.net

asp.net Problem Overview


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

asp.net Solutions


Solution 1 - asp.net

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();

Solution 2 - 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;

Solution 3 - asp.net

Like this:

DateTime.Now.ToString("MMMM yyyy")

For more information, see DateTime Format Strings.

Solution 4 - asp.net

    public string GetCurrentYear()
    {
        string CurrentYear = DateTime.Now.Year.ToString();

        return CurrentYear;
    }

    public string GetCurrentMonth()
    {
        string CurrentMonth = DateTime.Now.Month.ToString();

        return CurrentMonth;
    }

Solution 5 - asp.net

label1.Text = DateTime.Now.Month.ToString();

and

label2.Text = DateTime.Now.Year.ToString();

Solution 6 - asp.net

You can use this:

<p>&copy; <%: DateTime.Now.Year %> - My ASP.NET Application</p>

Solution 7 - asp.net

Find Current Year Using Razor try this

@DateTime.Now.Year

Solution 8 - asp.net

using System.Globalization;

LblMonth.Text = DateTime.Now.Month.ToString();

DateTimeFormatInfo dinfo = new DateTimeFormatInfo();
int month = Convert.ToInt16(LblMonth.Text);

LblMonth.Text = dinfo.GetMonthName(month);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionpreetyView Question on Stackoverflow
Solution 1 - asp.netdevil_coderView Answer on Stackoverflow
Solution 2 - asp.netEdwin de KoningView Answer on Stackoverflow
Solution 3 - asp.netSLaksView Answer on Stackoverflow
Solution 4 - asp.netIslam MohamedView Answer on Stackoverflow
Solution 5 - asp.netanishMarokeyView Answer on Stackoverflow
Solution 6 - asp.netToanTVView Answer on Stackoverflow
Solution 7 - asp.netAshuView Answer on Stackoverflow
Solution 8 - asp.netAnuragView Answer on Stackoverflow