1

我已将系统日期格式更改为 Faeroese。

我想根据customCulture将DateTime转换为String,G格式(日期和时间的组合)

检查下面的代码。

    namespace TestDateConvertion
        {
            class Program
            {
                static void Main(string[] args)
                {
                    object value = new DateTime(2003,12,23,6,22,30);
                    DateTime dateTimeValue = (DateTime)value;
                    CultureInfo customCulture = MySettings.getCustomCulture();  
                                     //for getting custom culture in my app
                                     //in custom culture i have changed shortDateFormat according to the user preference.
                                     //value in shortDateFormat = dd/MM/yyyy




                    string result = string.Format(customCulture, "{0:G}", result);

                    Console.WriteLine(result);
                    Console.ReadLine();
                }
            }
        }

但我根据系统 DateTime 而不是用户在 customCulture 中给定格式得到带有分隔符的输出,

我什至没有发现任何重载string.Format()DateTime.ToString()执行此操作的方法。

如果我通过 CultureInfo.InvariantCulture 那么我无法获得 G 格式的输出。

4

2 回答 2

1

尝试这个:

DateTime date1 = new DateTime(2008, 4, 10, 6, 30, 0);
Console.WriteLine(date1.ToString("G", DateTimeFormatInfo.InvariantInfo));
// Displays 04/10/2008 06:30:00
Console.WriteLine(date1.ToString("G", CultureInfo.CreateSpecificCulture("en-us")));
// Displays 4/10/2008 6:30:00 AM                        
Console.WriteLine(date1.ToString("G", CultureInfo.CreateSpecificCulture("nl-BE")));
于 2013-07-09T05:15:39.310 回答
1

根据标准日期和时间格式字符串“G”使用短日期格式(如您声称指定的那样)。因此,“自定义日期和时间格式字符串”的“/”自定义格式说明符部分涵盖了使用本地文化分隔符的最可能原因。

由于您的“短日期格式”"dd/MM/yyyy"不是"/"它将使用文化信息中的相应分隔符(您可能从默认文化中选择)。

\在同一“自定义日期和时间格式字符串”一文的使用转义字符部分中介绍了转义

因此,您希望在自定义的相应部分中shortDateFormat = @"dd\/MM\/yyyy"指定或正确指定DateTimeSeparatorCultureInfo

于 2013-07-09T05:16:06.580 回答