2010-10-08 34 views

Trả lời

23

Sử dụng thuộc tính DateTime.Now. Điều này trả về một đối tượng DateTime có chứa một thuộc tính Year và Month (cả hai đều là số nguyên).

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

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

Như thế này:

DateTime.Now.ToString("MMMM yyyy") 

Để biết thêm thông tin, xem DateTime Format Strings.

+0

Được sử dụng với mã nội tuyến rất đẹp và dễ dàng. <% = DateTime.Now.ToString ("MMMM yyyy")%> – atjoedonahue

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

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

Nếu bạn đã sau hai nhãn:

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

hơn bạn có thể sử dụng đoạn mã sau chỉ cần thiết lập các văn bản tài sản cho các nhãn như:

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); 
Các vấn đề liên quan