Is there a DateTimeFormatInfo
format pattern to convert a day of week to two characters? For example Tuesday becomes Tu, Wednesday becomes We. The format string needs to conform to the DateTimeFormatInfo
for date formats.
Addition:
Maybe I am looking for a solution to extend DateTimeFormatInfo
to include custom formats?
MMM/DD/YYYY. Three-letter abbreviation of the month, separator, two-digit day, separator, four-digit year (example: JUL/25/2003) YY/DDD. Last two digits of year, separator, three-digit Julian day (example: 99/349)
A date and time format string defines the text representation of a DateTime or DateTimeOffset value that results from a formatting operation. It can also define the representation of a date and time value that is required in a parsing operation in order to successfully convert the string to a date and time.
Go to the Number tab in the Format Cells dialog box. Select Custom as the Category. Add dddd into the Type field for the full weekday name or ddd for the abbreviated weekday name. Press the OK button.
You need to get the DateTimeFormatInfo
of the culture you're working with, then modify the array of strings called AbbreviatedDayNames
. After that, ddd
will return Th
for you.
http://msdn.microsoft.com/en-us/library/system.globalization.datetimeformatinfo.abbreviateddaynames(VS.71).aspx
DateTimeFormatInfo.AbbreviatedDayNames
Gets or sets a one-dimensional array of type String containing the culture-specific abbreviated names of the days of the week.
Here's a sample of how to do it:
class Program
{
static void Main()
{
var dtInfo = new System.Globalization.DateTimeFormatInfo();
Console.WriteLine("Old array of abbreviated dates:");
var dt = DateTime.Today;
for (int i = 0; i < 7; i++)
{
Console.WriteLine(dt.AddDays(i).ToString("ddd", dtInfo));
}
// change the short weekday names array
var newWeekDays =
new string[] { "Su", "Mo", "Tu", "We", "Th", "Fr", "Sa" };
dtInfo.AbbreviatedDayNames = newWeekDays;
Console.WriteLine("New array of abbreviated dates:");
for (int i = 0; i < 7; i++)
{
Console.WriteLine(dt.AddDays(i).ToString("ddd", dtInfo));
}
Console.ReadLine();
}
}
One more note: of course, if you are constrained from providing the IFormatProvider
, then you can override the current thread's CultureInfo
, for example:
CultureInfo customCulture = CultureInfo.CreateSpecificCulture("en-US");
// ... set up the DateTimeFormatInfo, etc...
System.Threading.Thread.CurrentThread.CurrentCulture = customCulture;
More on CurrentCulture:
http://msdn.microsoft.com/en-us/library/system.threading.thread.currentuiculture.aspx
Thread.CurrentUICulture Property
Gets or sets the current culture used by the Resource Manager to look up culture-specific resources at run time.
The closes you can get is the "ddd"
custom format specifier - this produces three lettered abbreviations, so not exactly what you want. There is nothing built in that does exactly what you want.
You can always take the first two characters of that:
DateTime.Now.ToString("ddd").Substring(0,2);
Unfortunately you can't extend DateTimeFormatInfo
since it is declared as sealed
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With