将日期转换为"dd-MMM-yyyy"格式c#

Apu*_*rba 15 c#-3.0

伙计我无法将日期时间转换为"dd-MMM-yyyy"格式.我的代码如下:

TreeNode tn = new TreeNode(dr["pBillDate"].ToString());       // Here i am getting both date and time.
Run Code Online (Sandbox Code Playgroud)

我需要将其转换为"2013年3月20日".

请帮我.谢谢.

Ras*_*ara 48

试试这段代码,

string formattedDate = YourDate.ToString("dd MMM yyyy");
Run Code Online (Sandbox Code Playgroud)

它会格式化为"2012年11月12日"


Bra*_*ley 8

以下几个例子应该有效:

DateTime dt = Convert.ToDateTime(dr["pBillDate"]);

TreeNode tn = new TreeNode(String.Format("{0:dd-MMM-yyyy}", dt));
Run Code Online (Sandbox Code Playgroud)

要么

DateTime dt = Convert.ToDateTime(dr["pBillDate"]);

TreeNode tn = new TreeNode(dt.ToString("dd-MMM-yyyy"));
Run Code Online (Sandbox Code Playgroud)


小智 6

DateTime StartDate = Convert.ToDateTime(datepicker.Text);
string Date = StartDate.ToString("dd-MMM-yyyy");
Run Code Online (Sandbox Code Playgroud)

  • 一些解释会有所帮助。 (4认同)