Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DateTime format in C#

Tags:

date

c#

asp.net

I have a simple problem: I want to write the date in an asp:label in the following format: MMM dd, yyyy I tried this:

lblDate.Text = System.DateTime.Today.ToString("MMM dd, yyyy");

the result is: "okt. 12, 2012"

  1. I want to remove the dot from it.
  2. The month's name should begin with a capital letter.

the right format: "Okt 12, 2012"

how can I do this?


2 Answers

You can use the AbbreviatedMonthNames property for this:

CultureInfo ci = CultureInfo.CreateSpecificCulture("en-US");
DateTimeFormatInfo dtfi = ci.DateTimeFormat;
dtfi.AbbreviatedMonthNames = new string[] 
{ 
  "Jan", "Feb", "Mar", 
  "Apr", "May", "Jun", 
  "Jul", "Aug", "Sep", 
  "Oct", "Nov", "Dec", "" 
}; 

lblDate.Text = DateTime.Now.ToString("MMM dd, yyyy", dtfi);

Then output will be Okt 12, 2012

like image 197
Erwin Avatar answered Jun 16 '26 19:06

Erwin


DateTime.ToString() formats the date according to the rules set out in the current culture. You can change it manually using the technique shown by @naspinski, but other users of your application may get different results, depending on their culture-specific settings. For instance, some cultures use a dot as a date separator, as in "12.10.2012". You can change your regional settings in Windows control panel to format how you like.

like image 27
Heretic Monkey Avatar answered Jun 16 '26 21:06

Heretic Monkey



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!