Section 19.5: DateTime.TryParse(String, DateTime) 메소드
// 날짜 및 시간을 나타내는 문자열을 DateTime 에 해당하는 객체로 변환시키고 변환이 성공했는지 여부를 나타내는 값을 반환한다.
string[] dateTimeStrings = new [] {
"14:23 22 Jul 2016",
"99:23 2x Jul 2016",
"22/7/2016 14:23:00"
};
foreach(var dateTimeString in dateTimeStrings) {
DateTime dateTime;
bool wasParsed = DateTime.TryParse(dateTimeString, out dateTime);
string result = dateTimeString +
(wasParsed
? $" was parsed to {dateTime}"
: " can't be parsed to DateTime");
Console.WriteLine(result);
}
출력 결과:
14:23 22 Jul 2016 was parsed to 7/22/2016 2:23:00 PM
99:23 2x Jul 2016 can't be parsed to DateTime
22/7/2016 14:23:00 can't be parsed to DateTime
본 문서는 C# Notes for Professionals (라이센스:CC-BY-SA) 를 한글로 번역한 문서입니다. 번역상 오류가 있을 수 있으므로 정확한 내용은 원본 문서를 참고하세요.
[출처] https://books.goalkicker.com/CSharpBook/
반응형
'번역 > C# Notes for Professionals' 카테고리의 다른 글
19.7: DateTime.Compare(DateTime t1, DateTime t2) 메소드 (0) | 2021.03.18 |
---|---|
19.6: DateTime.AddMilliseconds(Double) 메소드 (0) | 2021.03.18 |
19.4: DateTime.Parse(String) 메소드 (0) | 2021.03.17 |
19.3: DateTime.AddHours(Double) 메소드 (0) | 2021.03.17 |
19.2: DateTime.AddDays(Double) 메소드 (0) | 2021.03.17 |