2010-10-08 165 views

回答

23

使用DateTime.Now属性。这将返回一个包含Year和Month属性(均为整数)的DateTime对象。

string currentMonth = DateTime.Now.Month.ToString(); 
string currentYear = DateTime.Now.Year.ToString(); 

monthLabel.Text = currentMonth; 
yearLabel.Text = currentYear; 
13

像这样:

DateTime.Now.ToString("MMMM yyyy") 

欲了解更多信息,请参阅DateTime Format Strings

+0

与内联代码一起使用非常简单。 <%= DateTime.Now.ToString(“MMMM yyyy”)%> – atjoedonahue 2016-11-11 20:25:32

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

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

如果你有以下两个标签:

<asp:Label ID="MonthLabel" runat="server" /> 
<asp:Label ID="YearLabel" runat="server" /> 

比你可以使用下面的代码只需要设置文本属性为这些标签,如:

MonthLabel.Text = DateTime.Now.Month.ToString(); 
YearLabel.Text = DateTime.Now.Year.ToString(); 
0
using System.Globalization; 

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

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

LblMonth.Text = dinfo.GetMonthName(month); 
相关问题