2010-05-25 5 views

답변

3
DateTime.ParseExact("20100524", "yyyyMMdd", Thread.CurrentThread.CurrentCulture); 
+0

완벽! 감사합니다! –

+1

그 라인이 작동하지만, 더 나은 해결책은 여기 있습니다. –

3
DateTime result; 
CultureInfo provider = CultureInfo.InvariantCulture; 

string dateString = "20100524"; 
string format = "yyyyMMdd"; 
result = DateTime.ParseExact(dateString, format, provider); 
+0

물론, DateTime.Parse (string stringToParse); 작동합니다. http://msdn.microsoft.com/en-us/library/1k1skd40.aspx –

+0

유망한 것으로 보였으 나 유효한 날짜로 인식되지 않는다는 오류가 발생했습니다. –

+1

실제로 귀하의 것과 같은 사용자 지정 날짜 형식의 경우, ParseExact를 사용하십시오. –

6

DateTime.Parse 및 Datetime.ParseExact은 (는) 친구입니다.

16

이 안전한 방식으로 당신을 위해 그것을 할 것입니다 :

DateTime dateTime; 
if (DateTime.TryParseExact("20100524", "yyyyMMdd", null, DateTimeStyles.None, out dateTime)) 
{ 
    // use dateTime here 
} 
else 
{ 
    // the string could not be parsed as a DateTime 
} 
+0

+1 좋은 깨끗한 솔루션, –