how to get current month and year

前端 未结 8 637
南方客
南方客 2020-12-25 09:39

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

相关标签:
8条回答
  • 2020-12-25 09:52

    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;
    
    0 讨论(0)
  • 2020-12-25 10:01
    label1.Text = DateTime.Now.Month.ToString();
    

    and

    label2.Text = DateTime.Now.Year.ToString();
    
    0 讨论(0)
  • 2020-12-25 10:03
        public string GetCurrentYear()
        {
            string CurrentYear = DateTime.Now.Year.ToString();
    
            return CurrentYear;
        }
    
        public string GetCurrentMonth()
        {
            string CurrentMonth = DateTime.Now.Month.ToString();
    
            return CurrentMonth;
        }
    
    0 讨论(0)
  • 2020-12-25 10:05

    Find Current Year Using Razor try this

    @DateTime.Now.Year
    
    0 讨论(0)
  • 2020-12-25 10:10

    Like this:

    DateTime.Now.ToString("MMMM yyyy")
    

    For more information, see DateTime Format Strings.

    0 讨论(0)
  • 2020-12-25 10:10

    You can use this:

    <p>&copy; <%: DateTime.Now.Year %> - My ASP.NET Application</p>
    
    0 讨论(0)
提交回复
热议问题