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"
the right format: "Okt 12, 2012"
how can I do this?
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
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.
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