2012-05-12 13 views
6

2012年12月5日のような日付が1つあります。その形式を単純な文字列に変更したいと思います。日付を単語形式に変換するには?

ex。

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

今私のBDATEはDateTime すなわちです。 BDate= 2012/05/12

今私は私のBDATEは2012年12月5日 ある場合ので、私は「十二月2012年」のように似ている文字列をしたい

のような何かをしたい

私はどのように行うことができますこの?

+0

なぜこのようにしたいのですか?それはまれな日付形式です(そして、それは '5月十二日、二千十二日ではないでしょうか?)。 –

+3

@FrédéricHamidi:米国内のみ。世界の他の地域では、それを「12月」と発音しています。それが 'MM/dd'と' dd/MM'の相違の根底にある理由です。 – Douglas

+0

@Douglas、ああ、わかっています。あなたの明確化のためにありがとう:) –

答えて

9

各日付部分を見て、同等の機能を使用する必要があります。私はそれ以下のクラスが書かれたテキストに整数に変換含まれており、同様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

+0

あなたの大きな助けに感謝.. :) –

1

これはあなたが望むものではありませんが、私は組み込み機能を使用することをお勧めすることができ、最も近いがあなたを与える、ToLongDateStringです.... ...事前に

おかげで私を助けてください明らかに文化に敏感です。

string str = bdate.ToLongDateString(); 
// Assuming en-US culture, this would give: "Saturday, May 12, 2012" 
+0

私はこれが好きではありません。私は12月5日2千千を欲しい –

1

と仮定2012年12月5日には、それが「/」スラッシュで区切られた要素の中に、あなたはトークン化に持って、文字列です。例:

"2012年12月5日" - > [ "12"、 "05"、 "2012"]

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

+0

私は書かれた数字にそれらを変換するとOPが問題を抱えていると思います... –

関連する問題