2011-11-10 3 views
0

私は4〜6桁の文字列をC#の日付にどのように変換するのだろうと思っていましたか?C#の日付に文字列を変換する

111110 would be 11 11 10

111 10 would be 11 1 10

1 1 10 would be 1 1 10

パターンmmddyymmd yym d yy さとスペースが' 'または'\0'いずれかである(私は与えられていた入力が非常にきれいではありません)

この私がこれまでに持っているものとそれが働くもの上記のすべてのケースでは、それほどきれいではありません: 上記の場合より効率的なソリューションがありますか?

//Converts the given input string into a valid Date 
private DateTime convertToDateFromString(string dateString) 
{ 
    int length = dateString.Length; 
    int month = 1; 
    int day = 1; 
    int year = 1; 
    bool gotMonth = false; 
    bool gotDay = false; 
    bool gotYear = false; 
    char c = ' '; 
    char peek = ' '; 
    string buffer = ""; 
    DateTime bufferDate; 
    int count = 0; 

    try 
    { 
    //loop character by character through the string 
    for (int i = 0; i < dateString.Length; i++) 
    { 
     c = dateString[i]; 
     if ((i + 1) < dateString.Length) 
     peek = dateString[i + 1]; 
     else 
     peek = '\0'; 

     if (c != ' ' && c != '\0') 
     { 
     buffer += c; 
     count++; 
     //Check if the month is done 
     if ((peek == ' ' || peek == '\0' || count == 2) && gotMonth == false) 
     { 
      count = 0; 
      gotMonth = true; 
      month = int.Parse(buffer); 
      buffer = null; 
     } 
     //Check if the day is done 
     else if ((peek == ' ' || peek == '\0' || count == 2) && gotDay == false && gotMonth == true) 
     { 
      count = 0; 
      gotDay = true; 
      day = int.Parse(buffer); 
      buffer = null; 
     } 
     //Check if the year is done 
     else if ((peek == ' ' || peek == '\0' || count == 2) && gotYear == false && gotMonth == true && gotDay == true) 
     { 
      count = 0; 
      gotYear = true; 
      year = int.Parse(buffer); 
      buffer = null; 

      if (year >= 80 && year <= 99) 
      year += 1900; 
      else if (year >= 0 && year <= 79) 
      year += 2000; 
     } 
     } 
    } 
    bufferDate = new DateTime(year, month, day); 
    } 
    catch (System.Exception ex) 
    { 
    bufferDate = new DateTime(1, 1, 1); 
    } 
    return bufferDate; 
} 
+0

利用可能な機能について十分な調査をしていない状態から始めようとすると、これが起こります... – NexAddo

+0

1時間前に閉鎖された質問に対して私はそれとも新しい質問を書き直しましたか? http://stackoverflow.com/questions/8086209/convert-a-string-of-four-to-six-digits-to-a-date – Otiel

答えて

3

を開始する前に、あなたがここに識別器ができ

dateString = dateString.Replace('\0', ' '); 

DateTime.TryParseExact(dateString, new string[] { "MMddyy", "MMd yy", "M d yy" }, CultureInfo.CurrentCulture, DateTimeStyles.AssumeLocal, out result); 

を使用してみてくださいは、スペースの数です。まず、次のようにします。

string source = "....";  
int spaceCount = source.Count(c => c == ' '); 

次に予想される範囲0..2のフォーマットストリングを作成します。

var formats = new string[] { "MMddyy", "MMd yy", "M d yy" }; 

をして、あなたはあなたの日付を取得することができます:あなたは数ヶ月のためにMを使用する必要がありますことを除いて、あなたは質問から文字列を使用することができ、必要に応じ

DateTime r = DateTime.ParseExact(source, formats[spaceCount], null); 

は、検証を追加します。

3

あなたはDateTimeに定義された多くのParseのいずれかの方法を使用する必要があります。

これらは、文字列、オプションの書式文字列(日時文字列の書式を記述する)、およびオプションのカルチャを取ります。

ParseParseExactTryParseTryParseExactを見てみましょう、しようとする形式のstring[]を取るそのうちのいくつか。

さらに、ここで使用可能な文字列形式 - standardおよびcustomの日付と時刻の書式文字列。

+0

「DateTime.Parse」に使用できる文字列フォーマットのリストはありますか? – NexAddo

+0

@NexAddo - はい、あります。回答が修正されました。 – Oded

+0

問題は、彼が使用する必要のある多くの書式文字列があることです。 – McKay

3

あなたがする必要がある場合は

+0

これははるかに効率的な解決策です。ありがとう! – NexAddo

関連する問題