How to format a date as a localized string Short MonthDay

I would like to format the DateTime for a string containing the month abbreviation and the date the axis labels were used in the graph.

DateTime format strings do not contain an abbreviated month by default. I think there is no standard, but I could take a substring of the first three characters of the name of the month and replace it with the MonthDay format. The reason I will use MonthDay is because the order of the month and date depends on the locale.

Does anyone have a better idea?

http://msdn.microsoft.com/en-us/library/az4se3k1.aspx#MonthDay

+3
source share
6 answers

MonthDay "MMMM" "MMM" - :

string pattern = CultureInfo.CurrentCulture.DateTimeFormat.MonthDayPattern;
pattern = pattern.Replace("MMMM", "MMM");
string formatted = dateTime.ToString(pattern);

, , .

+15

:

DateTime now = DateTime.Now;
Console.WriteLine("{0}", now.ToString("ddd MMMM dd"));

. " " , .

+2

: date.ToString("d MMM"). 7 .

+1

CultureInfo Date.ToString(). , CultureInfo.CurrentCulture:

var date = DateTime.Now;
var ci = new CultureInfo("de-CH");
var output = date.ToString("d MMM", ci);
+1

, , 01/29:

Regex.Replace(c.DateTimeFormat.ShortDatePattern, @"(yy(yy)?[/\.\- ]|[/\.\- ]yy(yy)?)", "")

Here is the output format for all cultures, so you can see if it will work for you:

CultureInfo.GetCultures(CultureTypes.AllCultures)
    .Select(c => new {
        c.Name,
        c.DateTimeFormat.MonthDayPattern,
        c.DateTimeFormat.ShortDatePattern,
        DayMonth1 = Regex.Replace(c.DateTimeFormat.ShortDatePattern, @"[/\.\- ]?yy[/\.\- ]?", ""),
        DayMonth2 = Regex.Replace(c.DateTimeFormat.ShortDatePattern, @"(yyyy[/\.\- ]|[/\.\- ]yyyy|yy[/\.\- ]|[/\.\- ]yy)", ""),
        DayMonth3 = Regex.Replace(c.DateTimeFormat.ShortDatePattern, @"(yy(yy)?[/\.\- ]|[/\.\- ]yy(yy)?)", "")
    })
    .OrderBy(x => x.Name)
    .Dump();

Substitution DateMonth1will remove the extra fill character for a pair of crops (in particular, “bg”).

0
source

Quick, easy and does the job.

CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(Month).Substring(0,3);
0
source

Source: https://habr.com/ru/post/1744471/


All Articles