6

2012 年 12 月 5 日のような日付が 1 つあり、その形式を単純な文字列に変更したいと考えています。

例のために。

string newdate = new string();
newdate = "12/05/2012";
DateTime Bdate = DateTime.ParseExact(Newdate, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture);

今私のBDateはDateTime ieです。BDate= 2012/05/12

今、私は次のようなことをしたい

Bdate が 12/05/2012 の場合、「Twelve May two千十二」のような文字列が必要です。

これどうやってするの?

私を助けてください...

前もって感謝します....

4

4 に答える 4

11

各日付部分を見て、関数を使用して、書かれた同等のものを取得する必要があります。整数をテキストに変換するクラスを以下に含め、DateTime変換もサポートするように拡張しました。

public static class WrittenNumerics
{
    static readonly string[] ones = new string[] { "", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine" };
    static readonly string[] teens = new string[] { "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen" };
    static readonly string[] tens = new string[] { "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety" };
    static readonly string[] thousandsGroups = { "", " Thousand", " Million", " Billion" };

    private static string FriendlyInteger(int n, string leftDigits, int thousands)
    {
        if (n == 0)
            return leftDigits;

        string friendlyInt = leftDigits;
        if (friendlyInt.Length > 0)
            friendlyInt += " ";

        if (n < 10)
            friendlyInt += ones[n];
        else if (n < 20)
            friendlyInt += teens[n - 10];
        else if (n < 100)
            friendlyInt += FriendlyInteger(n % 10, tens[n / 10 - 2], 0);
        else if (n < 1000)
            friendlyInt += FriendlyInteger(n % 100, (ones[n / 100] + " Hundred"), 0);
        else
            friendlyInt += FriendlyInteger(n % 1000, FriendlyInteger(n / 1000, "", thousands + 1), 0);

        return friendlyInt + thousandsGroups[thousands];
    }

    public static string DateToWritten(DateTime date)
    {
        return string.Format("{0} {1} {2}", IntegerToWritten(date.Day), date.ToString("MMMM"), IntegerToWritten(date.Year));
    }

    public static string IntegerToWritten(int n)
    {
        if (n == 0)
            return "Zero";
        else if (n < 0)
            return "Negative " + IntegerToWritten(-n);

        return FriendlyInteger(n, "", 0);
    }
}

免責事項:基本機能は@Wedge 提供

このクラスを使用して、DateToWritten メソッドを呼び出すだけです。

var output = WrittenNumerics.DateToWritten(DateTime.Today);

上記の出力は次のとおりです。Twelve May Two Thousand Twelve

于 2012-05-12T17:38:12.353 に答える
2

これはあなたが望むものではありませんが、組み込み機能を使用して提案できる最も近いのは ですToLongDateString。これは、月の名前を提供し、明らかにカルチャに依存します。

string str = bdate.ToLongDateString();
// Assuming en-US culture, this would give: "Saturday, May 12, 2012"
于 2012-05-12T17:24:18.147 に答える
1

12/05/2012 が文字列であると仮定すると、スラッシュ "/" で区切られた要素にトークン化する必要があります。例えば:

"12/05/2012" -> ["12", "05", "2012"]

次に、これらの要素を期待どおりに解析するルールを自分で定義します。たとえば、「12」は「12」、「05」は「5」または「5 月」などです。

于 2012-05-12T17:30:52.540 に答える