繁体   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