you can use DateTime's ToString() or String.Format(), for exampleusing System;DateTime dt = DateTime.Now;
Console.WriteLine(dt.ToString("yyyy-MM-dd hh:mm:ss"));
String s = String.Format("{0:yyyy-MM-dd}",dt);
Console.WriteLine(s);

解决方案 »

  1.   

    Format方法的格式是: public string Format( 
    string format, 
    IServiceObjectProvider sp 
    );  format是指我们指定的格式,它的取值有很多,我得列表:) 标准的Format格式Format Format 模式 
    d MM/dd/yyyy 如(2001-3-27) 
    D dddd, MMMM dd, yyyy 如(2001年3月27日) 
    f dddd, MMMM dd, yyyy HH:mm 如(2001年3月27日 0:00) 
    F dddd, MMMM dd, yyyy HH:mm:ss 如(2001年3月27日 0:00:00) 
    g MM/dd/yyyy HH:mm 如(2001-3-27 0:00) 
    G MM/dd/yyyy HH:mm:ss 如(2001-3-27 0:00:00) 
    m, M MMMM dd 如(三月 27) 
    r, R ddd, dd MMM yyyy HH’:’mm’:’ss ’GMT’ 如(Mon, 26 Mar 2001 16:00:00 GMT) 
    s yyyy-MM-dd HH:mm:ss 好像不能使用 
    t HH:mm 如(0:00) 
    T HH:mm:ss 如(0:00:00) 
    u yyyy-MM-dd HH:mm:ss 如(2001-03-26 16:00:00Z) 
    U dddd, MMMM dd, yyyy HH:mm:ss 如(2001年3月26日 16:00:00) 
    y, Y MMMM, yyyy 如(2001年3月) 自定义格式列表Format Pattern Description 
    d 如:2001-3-27 
    dd 如:27 
    ddd 如:星期一 
    dddd 如:星期一(全名) 
    M 如:三月 27 
    MM 如:03 
    MMM 如:三月 
    MMMM 如:三月(全称) 
    y 如:2001年3月 
    yy 如:01 
    yyyy 如:2001 
    gg 如:A.D. 
    hh, hh* 如:12 
    HH, HH* 如:00 
    m 如:三月 27 
    mm, mm* 如:00(分钟) 
    s 如:2001-03-27T00:00:00 
    ss, ss* 如:00(秒) 
    t 如:0:00 
    tt, tt* 如:上午   下面是一个简单操作的例子。 <% @ Page Language="C#" %> 
    <% @ Import Namespace="System.Data" %> 
    <Script Language="C#" Runat="Server"> 
    public void Page_Load(Object src,EventArgs e) 

    DateTime dt = DateTime.Now; 
    ctime.Text=dt.Format("MM",null).ToString(); 

    </script> 
    <html> 
    <head> 
    <title></title> 
    </head> 
    <body> 
    <asp:Label id="ctime" runat="server" /> 
    </body> 
    </html> 你还可以看看帮助。
      

  2.   

    类似C中的sprintf,例如:
    int i=10;
    string result = String.Format( "The value is {0}.", i );
    Console.WriteLine( result );Output:
    The value is 10.函数声明:
    public static string Format(
       string format,
       params object[] args
    );
    注意:object如果实现了IFormattable接口的话,那么将会被调用object.Format方法来输出,否则将调用object.ToString()来输出。MSDN上有很详细的说明,供参阅。