Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DateTimeFormatInfo string format for day of week. Thursday becomes Th

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?

like image 503
Tony_Henrich Avatar asked Jun 16 '10 19:06

Tony_Henrich


People also ask

What is format mmm dd yyyy?

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)

What is string date/time format?

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.

How do you extract the day of the week from a specific date?

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.


2 Answers

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.

like image 180
code4life Avatar answered Sep 29 '22 12:09

code4life


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.

like image 31
Oded Avatar answered Sep 29 '22 11:09

Oded