Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# datetime format that includes date/month/year and not day

Tags:

c#

datetime

I want 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.

like image 439
bschandramohan Avatar asked Mar 09 '26 19:03

bschandramohan


2 Answers

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", ""));
like image 133
viky Avatar answered Mar 12 '26 07:03

viky


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.

like image 23
Jon Skeet Avatar answered Mar 12 '26 07:03

Jon Skeet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!