6

フランスの顧客の日付をフォーマットしようとしています。

次の例に示すようにTimesをフォーマットする必要があります...

06:00->6時間08:
45->8時間4510 :
30->10時間3015:00-
>15時間
17:22->17時間2218:00-
>18時間

カスタムの日付と時刻の書式設定を使用できました。しかし、私は、フランス語(少なくともカナダ人)がゼロ「00」の場合、議事録を表示しないというこの表記に固執しているようです。

現在、以下のフォーマットを使用しています。

myDateTime.ToString("H \h mm")

「mm」が00より大きい場合にのみ表示されるようにするにはどうすればよいですか?

拡張メソッドやプロキシクラスはフレームワークに組み込む必要があると思うので、使用を避けたいと思います。どうやらフランス時代の標準フォーマット。

私の文字列"H\ h mm"は、実際にはリソースファイルからのものです。すなわち..。

myDateTime.ToString(Resources.Strings.CustomTimeFormat);
4

5 に答える 5

4

悪い知らせがあります。フレームワークはあなたが探しているフォーマットをサポートしていません。次のコードはこれを証明しています:

using System;
using System.Globalization;

namespace ConsoleApplication1
{
    public class Program
    {
        static void Main(string[] args)
        {
            // FR Canadian
            Console.WriteLine("Displaying for: fr-CA");
            DisplayDatesForCulture("fr-CA");

            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine();

            // FR French
            Console.WriteLine("Displaying for: fr-FR");
            DisplayDatesForCulture("fr-FR"); 

            Console.WriteLine();
            Console.WriteLine("Press enter to exit.");
            Console.ReadLine();
        }

        static void DisplayDatesForCulture(string culture)
        {
            var ci = CultureInfo.GetCultureInfo(culture);
            var dt = new DateTime(2010, 10, 8, 18, 0, 0);

            foreach (string s in ci.DateTimeFormat.GetAllDateTimePatterns())
                Console.WriteLine(dt.ToString(s));
        }
    }
}

アプリは、サポートされているすべての日時形式を表示します。それらのどれも18:00==>18時間などの概念をサポートしていません。

最善のオプションは、拡張メソッドまたは同様のアプローチを作成することです。

文化に敏感なアプローチ:拡張ヘルパークラスを構築します。

public static class DateHelper
{
    public static string ToLocalizedLongTimeString(this DateTime target)
    {
        return ToLocalizedLongTimeString(target, CultureInfo.CurrentCulture);
    }

    public static string ToLocalizedLongTimeString(this DateTime target, 
        CultureInfo ci)
    {
        // I'm only looking for fr-CA because the OP mentioned this 
        // is specific to fr-CA situations...
        if (ci.Name == "fr-CA")
        {
            if (target.Minute == 0)
            {
                return target.ToString("H' h'");
            }
            else
            {
                return target.ToString("H' h 'mm");
            }
        }
        else
        {
            return target.ToLongTimeString();
        }
    }
}

あなたはそのようにテストすることができます:

var dt = new DateTime(2010, 10, 8, 18, 0, 0);

// this line will return 18 h
Console.WriteLine(dt.ToLocalizedLongTimeString(CultureInfo.GetCultureInfo("fr-CA")));

// this line returns 6:00:00 PM
Console.WriteLine(dt.ToLocalizedLongTimeString());

var dt2 = new DateTime(2010, 10, 8, 18, 45, 0);

// this line will return 18 h 45
Console.WriteLine(dt2.ToLocalizedLongTimeString(CultureInfo.GetCultureInfo("fr-CA")));

// this line returns 6:45:00 PM
Console.WriteLine(dt2.ToLocalizedLongTimeString());
于 2010-10-08T20:28:32.197 に答える
2

code4lifeの拡張メソッドのアイデアに続いて、ここに拡張メソッドがあります。= p

public static string ToCanadianTimeString(this DateTime source)
{
    if (source == null)
        throw new ArgumentNullException("source");

    if (source.Minute > 0)
        return String.Format("{0:hh} h {0:mm}", source);

    else
        return String.Format("{0:hh} h", source);
}
于 2010-10-08T20:46:50.893 に答える
1

これが私が行っている解決策です:

public static string ToStringOverride(this DateTime dateTime, string format)
{
    // Adjust the "format" as per unique Culture rules not supported by the Framework
    if (CultureInfo.CurrentCulture.LCID == 3084) // 3084 is LCID for fr-ca
    {
        // French Canadians do NOT show 00 for minutes.  ie.  8:00 is shown as "8 h" not "8 h 00"
        if (dateTime.Minute == 0)
        {
            format = format.Replace("mm", string.Empty);
        }
    }

    return dateTime.ToString(format);
}

少なくとも、次のようなさまざまなDateTime形式で機能します...

  • H \ h mm
  • dddd d MMMM yyyy H \ h mm tt

ここでの考え方は、分がゼロの場合に「mm」を削除するようにFormatStringを変更することです。それでもフレームワークに大変な仕事をさせます。

于 2010-10-08T22:10:40.003 に答える
1

かわいくないかもしれませんが、彼らが主張するなら

if (Locality == france)
myDateTime.ToString(Resources.Strings.CustomTimeFormat).Replace("00","") ;
else 
myDateTime.ToString(Resources.Strings.CustomTimeFormat);
于 2010-10-08T20:03:54.737 に答える
0

これもきれいではありません:

myDateTime.ToString(myDateTime.Minute == 0 ? @"H \h" : @"H \h mm")
于 2012-08-13T15:55:46.947 に答える