C# - 如何使用DateTime.ToString显示Apr '11

Mik*_*sen 17 .net c#

好的,我已经尝试了用这种方法以4月'11格式呈现日期的所有内容DateTime ToString.文档说'是为字符串文字保留的,所以我想要显示我使用的单个撇号'''- 但是,没有去.这是我到目前为止所尝试的:

taskdata.Month.Start.ToString("MMM 'yy")
A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
taskdata.Month.Start.ToString("MMM ''yy")
"Apr 09"
taskdata.Month.Start.ToString("MMM '''yy")
A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
taskdata.Month.Start.ToString("MMM ''''yy")
"Apr 09"
taskdata.Month.Start.ToString("MMM \'yy")
A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
taskdata.Month.Start.ToString("MMM '\''yy")
A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
Run Code Online (Sandbox Code Playgroud)

说真的是什么秘密?我拒绝连接!

Kir*_*oll 42

你需要逃脱(实际上是双重逃避):

new DateTime(2011, 10, 1).ToString("MMM \\'yy")
Run Code Online (Sandbox Code Playgroud)

  • 请注意,如果使用逐字字符串文字,则不需要双重转义:`@"MMM \'yy"`. (15认同)

BZi*_*ink 10

String.Format(@"{0:MMM  \'yy}", DateTime.Now)
Run Code Online (Sandbox Code Playgroud)

为我工作