簡體   English   中英

C#DateTime ToString標准文化格式

[英]C# DateTime ToString Standard culture format

我可以為特定文化更改DateTime的標准輸出格式。 例:

class Program
{
    static void Main(string[] args)
    {
        PrintCultureDateTime("ca-ES");
        PrintCultureDateTime("es-ES");
        PrintCultureDateTime("en-US");
        PrintCultureDateTime("en-GB");
        PrintCultureDateTime("pt-PT");
    }

    public static void PrintCultureDateTime(string culture)
    {
        string result = new DateTime(2017, 10, 1).ToString("d", 
            CultureInfo.GetCultureInfo(culture));
        Console.WriteLine(string.Format("{0}: {1}", culture, result));
    }
}

輸出:

ca-ES: 1/10/2017
es-ES: 01/10/2017
en-US: 10/1/2017
en-GB: 01/10/2017
pt-PT: 01/10/2017

我想要的是使用標准“d”格式或其他標准格式的日期和月份兩位數字,以便修復DateTime字符串的大小。 像那樣的輸出:

ca-ES: 01/10/2017
es-ES: 01/10/2017
en-US: 10/01/2017
en-GB: 01/10/2017
pt-PT: 01/10/2017

您可以使用DateTimeFormatInfo.ShortDatePattern來獲取模式,然后如果您真的想用dd替換d 您可能需要考慮轉角情況,例如轉義或引用的d模式 - 以及顯然不會用dddd替換dd (請注意,來自系統文化的格式信息將是只讀的;克隆文化以獲得具有讀/寫格式信息的格式。)當然,您也可以使用相同的數月。 您甚至可能需要更改年份部分。

我不清楚這樣做是個好主意 - 當你改變格式時,它不再是“文化的標准短日期格式”了。

即使在所有這些之后,也無法保證所有文化的長度都相同。 沒有什么可以阻止文化使用短日期格式"'Year:' yyyy '; Month: ' MM '; 'Day: 'dd" 這將是非常不尋常的,但不是無效的。

像這樣的東西可能適用於大多數日期格式,但正如Jon Skeet所說,可能有一些奇怪的格式很難處理。

public class CustomFormatProvider : IFormatProvider
{
    private string _culture;

    public CustomFormatProvider(string culture)
    {
        _culture = culture;
    }

    public object GetFormat(Type formatType)
    {
        if (formatType.Equals(typeof(DateTimeFormatInfo)))
        {
            var format = 
CultureInfo.GetCultureInfo(_culture).DateTimeFormat;
            var info = new DateTimeFormatInfo();
            switch (format.ShortDatePattern.ToString())
            {
                case "d/M/yyyy":
                case "d/MM/yyyy":
                case "dd/M/yyyy":
                case "dd/MM/yyyy":
                    info.ShortDatePattern = "dd/MM/yyyy";
                    return info;
                case "M/dd/yyyy":
                case "MM/dd/yyyy":
                case "M/d/yyyy":
                case "MM/d/yyyy":
                    info.ShortDatePattern = "MM/dd/yyyy";
                    return info;
                default:
                    //This is just example for default handling
                    info.ShortDatePattern = "dd/MM/yyyy";
                    return info;
            }
        }
        else return null;
    }
}

用法:

public static void PrintCultureDateTime(string culture)
    {
        string result = new DateTime(2017, 10, 1).ToString("d", new 
CustomFormatProvider(culture));
        Console.WriteLine(string.Format("{0}: {1}", culture, result));
    }

輸出:

在此輸入圖像描述

基於@Jon Skeet的回答我已經實現了以下解決方案。 我認為必須存在一個具有單次迭代的更好的正則表達式替換指令,但我沒有更多的時間來找到它。

它不適用於格式“'年:'yyyy';月:'MM';'日:'dd”,但我會假設“錯誤”。

    public static void PrintCultureDateTime(string culture)
    {
        string result = new DateTime(2017, 10, 1).ToString(FixedSizeShortDatePattern(culture));
        Console.WriteLine(string.Format("{0}: {1}", culture, result));
    }

    public static string FixedSizeShortDatePattern(string culture)
    {
        var cultureInfo = CultureInfo.GetCultureInfo(culture);
        string format = cultureInfo.DateTimeFormat.ShortDatePattern;
        return ReplaceSingleChar(ReplaceSingleChar(format, 'd'), 'M');
    }

    public static string ReplaceSingleChar(string input, char c)
    {
        string cStr = c.ToString();
        string strRegex = string.Format(@"^({0})[^{0}]|[^{0}]({0})[^{0}]|[^{0}]({0})$", cStr);
        return Regex.Replace(input, strRegex, (match) =>
        {
            string token = match.Groups[0].Value;
            if (!string.IsNullOrEmpty(token))
            {
                return match.Value.Replace(cStr, string.Concat(cStr, cStr));
            }
            return token;
        });
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM