2010-10-08 89 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); 
相關問題