C# 如何基于区域性仅显示日期和月份的日期字符串?

C# 如何基于区域性仅显示日期和月份的日期字符串?,c#,windows-phone-7,datetime,windows-phone-7.1,datetime-format,C#,Windows Phone 7,Datetime,Windows Phone 7.1,Datetime Format,我需要在windows phone上只显示日期和月份 例如,1月1日,对于美国用户,我希望文本为“1/31”,对于英国用户,我希望文本为“31/1” 实现这样的目标最简单的方法是什么 更新: 我的应用程序将在许多国家提供。我是否必须指定每种文化才能得到我想要的 比如说, de-DE Culture 01.10 en-US Culture 10/1 es-ES Culture

我需要在windows phone上只显示日期和月份

例如,1月1日,对于美国用户,我希望文本为“1/31”,对于英国用户,我希望文本为“31/1”

实现这样的目标最简单的方法是什么

更新: 我的应用程序将在许多国家提供。我是否必须指定每种文化才能得到我想要的

比如说,

de-DE Culture                         01.10
en-US Culture                         10/1
es-ES Culture                         01/10
fr-FR Culture                         01/10
左栏包含我的应用程序将支持的一些国家,右栏是我希望我的日期文本的格式

有什么通用的方法可以做到这一点吗

// 13 August
string americanDate = theDate.ToString("m", new CultureInfo("en-us"));

// August 13
string ukDate = theDate.ToString("m", new CultureInfo("en-gb"));
有关可用格式的完整列表,请参阅

编辑:

在应用程序中,不要手动指定
CultureInfo
实例:使用
DateTime.ToString
的默认区域性,这是执行线程的区域性

因此,您的代码变成:

string cultureSpecificDate = theDate.ToString("m");
其中,
“m”
是您在可用格式列表中选择的日期格式。您想要的似乎不是该类提供的标准。您不能在下面的列表中选择支持的模式吗

/*
This code produces the following output.

FORMAT  en-US EXAMPLE
CHAR    VALUE OF ASSOCIATED PROPERTY, IF ANY

  d     1/3/2002
        M/d/yyyy (ShortDatePattern)

  D     Thursday, January 03, 2002
        dddd, MMMM dd, yyyy (LongDatePattern)

  f     Thursday, January 03, 2002 12:00 AM

  F     Thursday, January 03, 2002 12:00:00 AM
        dddd, MMMM dd, yyyy h:mm:ss tt (FullDateTimePattern)

  g     1/3/2002 12:00 AM

  G     1/3/2002 12:00:00 AM

  m     January 03
        MMMM dd (MonthDayPattern)

  M     January 03
        MMMM dd (MonthDayPattern)

  o     2002-01-03T00:00:00.0000000

  r     Thu, 03 Jan 2002 00:00:00 GMT
        ddd, dd MMM yyyy HH':'mm':'ss 'GMT' (RFC1123Pattern)

  R     Thu, 03 Jan 2002 00:00:00 GMT
        ddd, dd MMM yyyy HH':'mm':'ss 'GMT' (RFC1123Pattern)

  s     2002-01-03T00:00:00
        yyyy'-'MM'-'dd'T'HH':'mm':'ss (SortableDateTimePattern)

  t     12:00 AM
        h:mm tt (ShortTimePattern)

  T     12:00:00 AM
        h:mm:ss tt (LongTimePattern)

  u     2002-01-03 00:00:00Z
        yyyy'-'MM'-'dd HH':'mm':'ss'Z' (UniversalSortableDateTimePattern)

  U     Thursday, January 03, 2002 8:00:00 AM

  y     January, 2002
        MMMM, yyyy (YearMonthPattern)

  Y     January, 2002
        MMMM, yyyy (YearMonthPattern)

*/

哇,这个问题特别有趣!我没有找到什么明显的解决办法,所以这里是:

    char[] trimmer = DateTimeFormatInfo.CurrentInfo.DateSeparator.ToCharArray();
    string dateStr = date.ToString("d").Replace(date.ToString("yyyy"), string.Empty).Trim(trimmer);
如果您不能使用
DateTimeFormatInfo
,那么下面的代码应该可以做到这一点:

string tmp = date.ToString("d").Replace(date.ToString("yyyy"), string.Empty);
char last = tmp[tmp.Length - 1];
char[] trimmer = char.IsDigit(last) ? new char[] { tmp[0] } : new char[] { last };
string dateStr = tmp.Trim(trimmer);
正如正确提到的,这个解决方案也有缺陷(比我实际预期的要多)。以下是Complete测试套件():

进一步的优化是可能的(至少在完整的框架中),但是它变得相当复杂(Windows Phone的有限框架也增加了复杂性),并且不清楚这种日期表示在某些文化中是否有意义。例如,在某些区域性中,日期部分分隔符是两个字符的序列:

胡虎,2012年。814. => 8十四,


没有
08。14
真的好看吗?或者应该是
08。14.
?我不确定。如果我是你,我真的想处理最大数量的文化,我会坚持使用标准的“d”格式说明符

在解析日期时间时不能指定CultureInfo吗?+1我喜欢你的答案!但是,我无法使用微调器函数,因为wp7不支持日期分隔符:(但修复起来很容易,我可以使用子字符串函数去掉最后一个字符。如果我在几个小时内没有收到更好的答案,我很乐意接受你的答案。:)你不应该删除最后一个字符,因为一年可以在一个月之前到来(例如在日本)。请参阅更新的答案。您不应该使用字符串函数(如
Replace
Trim
…等)处理格式化日期。有很多不同的文化形式,你不能把所有的情况都考虑进去。例如,您的代码假定日期为
。对于所有区域性,ToString(“d”)
总是在
yyyy
表单中包含年份,这是不正确的(例如,测试
“kn in”
。@ken2k我知道这一事实(这就是为什么这个问题很有趣),但是没有完美的解决方案。
m
格式产生不同于所需的输出,因此它只是一个合理通用的解决方案。如有必要,特殊培养病例可单独处理。@ken2k感谢您对此的澄清。。。你能告诉我在哪里可以找到拥有自己日期时间字符串的国家的完整列表,这样我就可以编写一个function来过滤它们吗?@Jessica嗯,如果你不选择标准模式,我不确定你的问题是否有一个实际的通用解决方案(除了必须为每个受支持的文化编写特定代码)。您给出的示例(de、en、es、fr)可以定义为没有年份的模式,但对于其他更“异国”的文化,预期的结果是什么?只有定义良好的模式(即上面列表中的模式)才能确保所有区域性的输出。
using System;
using System.Diagnostics;
using System.Globalization;

class Program
{
    static void Main(string[] args)
    {
        CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.SpecificCultures);

        DateTime date = DateTime.Now;

        foreach (CultureInfo culture in cultures)
        {
            string tmp = date.ToString("d", culture).Replace(date.ToString("yyyy", culture), string.Empty);
            char last = tmp[tmp.Length - 1];
            char[] trimmer = char.IsDigit(last) ? new char[] { tmp[0] } : new char[] { last };
            string dateStr = tmp.Trim(trimmer);

            Console.WriteLine(string.Format("{0,12}, {1,15} => {2,10}", culture.IetfLanguageTag, date.ToString("d", culture), dateStr));
        }

        if (Debugger.IsAttached)
        {
            Console.WriteLine();
            Console.WriteLine("Press any key to exit..");
            Console.ReadKey();
        }
    }
}