C#: DateTime.ToString() Format – Apostrophe Before the Year

I was trying to use the DateTime.ToString(string) method to create a string that had the abbreviated month and last two digits of the year but I wanted to add an apostrophe before the year to produce something like Sep ’10 for September 2010. It took me a while to figure our the correct escaping sequence but here it is.

DateTime date = DateTime.Today;
Console.WriteLine(date.ToString("MMM \"'\"yy"));

// Output
// ------
// Sep '10

Any text between the two escaped quotation marks (\”…\”) will be printed as literal text.


2 Responses to “C#: DateTime.ToString() Format – Apostrophe Before the Year”

  1. Dan Says:

    Great solution to a painful problem. Thank you for sharing đŸ™‚

  2. Ten Speed Software (@TenSpeedSoft) Says:

    4.5 years later, still helpful. Thx for posting.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: