2013-03-14 4 views

답변

2
// this is your original string 
string _str = "20130101"; 

// you need to convert it to valid DateTime datatype 
// so you can freely format the string to what you want 
DateTime _date = DateTime.ParseExact(_str, "yyyyMMdd", CultureInfo.InvariantCulture); 

// converting to your desired format, which is now a string 
string _dateStr = _date.ToString("yyyy-MM-dd"); 
2

당신은 DateTime을 구문 분석해야 다음 다시 포맷 :

var input = ... 
var inFormat = "yyyyMMdd"; 
var outFormat = "yyyy-MM-dd"; 
var date = DateTime.ParseExact(inFormat, input, CultureInfo.InvariantCulture); 
var output = date.ToString(outFormat); 
1

안전한 방법은 예를 들면, 날짜 시간 개체로 변환하는 것입니다 .Net 아래 함수를 사용 :

DateTime.TryParseExact() 

그리고 DateTime 개체를 사용하여 다시 포맷 할 수 있습니다. 같은 예를 아래 : http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

: 자세한 내용은

dateTimeObject.ToString(YourFormatInString); 

체크 MSDN

관련 문제