tags:

views:

353

answers:

4

Another silly question, but I wanted to use a standard date format that displays date, month and year in the standard regional settings of the pc. However I could only find "D" which lists day along with Date-Month-Year. Is there any way I can remove the day from it or any other way to get my desired output?

DateTime date1 = new DateTime(2008, 4, 10);
Console.WriteLine(date1.ToString("D", 
                  CultureInfo.CreateSpecificCulture("en-US")));
// Displays Thursday, April 10, 2008

Note: I don't want to be using custom format (d MMMM yyyy) as I want the regional settings of the order to be maintained.

Thanks for any inputs.

+4  A: 

try this:

date1.ToString("d", CultureInfo.CreateSpecificCulture("en-US"))

It will return what you want!!

viky
I want the output to be same as in D without the "Thursday"... So it should be "April 10, 2008" and the order of April 10 and 2008 should be based on the local settings. So, can't use "d" and using specific culture isn't also an acceptable way for us :(
Chandra Mohan
+3  A: 

You should use "d" instead of "D" to get the desired output.

treaschf
I want the output to be same as in D without the "Thursday"... So it should be "April 10, 2008" and the order of April 10 and 2008 should be based on the local settings. So, can't use "d" :(
Chandra Mohan
It seems that you don't have a standard format string for that. You have to use a custom format string. However you can put that format string into a resource, and this way you can ensure, that you have the appropriate order for the different cultures your application supports.
treaschf
An other approach would be the one presented by Jon Skeet - to look up the format string from the DateTimeFormatInfo, and remove the name of the day from it.
treaschf
+4  A: 

If you're using one of the standard formats, then what gets displayed is going to depend on the culture anyway - so even if you could find something which doesn't display the day of week in en-US, it may still display it in other cultures.

I suppose you could find the DateTimeFormatInfo for the culture, find its LongDatePattern and then remove any occurrence of a single "d" from that format string. It would be pretty nasty though.

Jon Skeet
+2  A: 

you can use this for your case:

DateTimeFormatInfo myDTFI = new CultureInfo("en-US", false).DateTimeFormat;
string str = (new DateTime(2008, 4, 10)).ToString(myDTFI.LongDatePattern.Replace("dddd", ""));
viky