DateTime In C#
Here is a detailed tutorial on C# DateTime class and how to work with dates and times using C#.
www.c-sharpcorner.com
Value : 날짜와 시간의 문자열 표현입니다.
Provider: 문화(culture)별 정보를 제공하는 개체입니다.
CultureInfo culture = new CultureInfo("en-US");
DateTime tempDate = Convert.ToDateTime("1/1/2010 12:10:15 PM", culture);
// Convert.ToDateTime()
string dateString = null;
// null 을 변환합니다.
DateTime dateTime10 = Convert.ToDateTime(dateString); // 1/1/0001 12:00:00 AM
dateString = "not a date";
// Exception: 문자열이 유효한 DateTime으로 인식되지 않았습니다.
// 색인 0에서 시작하는 알 수 없는 단어가 있습니다.
DateTime dateTime11 = Convert.ToDateTime(dateString);
dateString = "Tue Dec 30, 2015";
// Exception: 문자열은 요일이 틀렸기 때문에 유효한 DateTime으로 인식되지 않았습니다.
DateTime dateTime12 = Convert.ToDateTime(dateString);
string dateString = null;
// Exception: Argument null exception
DateTime dateTime10 = DateTime.Parse(dateString);
dateString = "not a date";
// Exception: The string was not recognized as a valid DateTime.
// There is an unknown word starting at index 0.
DateTime dateTime11 = DateTime.Parse(dateString);
dateString = "Tue Dec 30, 2015";
// Exception: String was not recognized as a valid DateTime because the day of week was incorrect.
DateTime dateTime12 = DateTime.Parse(dateString);
string dateString = null;
CultureInfo provider = CultureInfo.InvariantCulture;
// null 값은 오류를 발생시킵니다.
DateTime dateTime10 = DateTime.ParseExact(dateString, "mm/dd/yyyy", provider);
dateString = "not a date";
// 날짜 형태가 아니므로 오류 발생시킵니다.
// Exception: The string was not recognized as a valid DateTime. There is an unknown word starting at index 0.
DateTime dateTime11 = DateTime.ParseExact(dateString, "mm/dd/yyyy", provider);
dateString = "Tue Dec 30, 2015";
//문자열 날짜 형식이 mm/dd/yyyy 아니므로 오류 발생 시킵니다.
// Exception: String was not recognized as a valid DateTime because the day of week was incorrect.
DateTime dateTime12 = DateTime.ParseExact(dateString, "mm/dd/yyyy", provider);
dateString = "10-22-2015";
// 10/22/2015 12:00:00 AM 형식으로 반환됩니다.
DateTime dateTime13 = DateTime.ParseExact(dateString, "MM-dd-yyyy", provider);
string temp = dateTime13.ToString();
dateString = "10-12-2015";
// dateString 변수의 값인 "10-12-2015"는 "MM.dd.yyyy", "MM-dd-yyyy", "MM/dd/yyyy" 형식 중 하나에 해당하므로 변환이 가능
// 따라서 dateTime16 변수에는 "2015-10-12 00:00:00"과 같은 DateTime 값이 저장됩니다.
DateTime dateTime16 = DateTime.ParseExact(dateString, new string[] { "MM.dd.yyyy", "MM-dd-yyyy", "MM/dd/yyyy" }, provider, DateTimeStyles.None);
string dateString = null;
// Convert a null string.
DateTime dateTime10;
bool isSuccess = DateTime.TryParse(dateString, out dateTime10); // 1/1/0001 12:00:00 AM
dateString = "not a date";
DateTime dateTime11;
bool isSuccess1 = DateTime.TryParse(dateString, out dateTime11); // 1/1/0001 12:00:00 AM
dateString = "Tue Dec 30, 2015";
DateTime dateTime12;
bool isSuccess2 = DateTime.TryParse(dateString, out dateTime12); // 1/1/0001 12:00:00 AM
string dateString = null;
CultureInfo provider = CultureInfo.InvariantCulture;
DateTime dateTime10; // 1/1/0001 12:00:00 AM
bool isSuccess1 = DateTime.TryParseExact(dateString, "MM/dd/yyyy", provider, DateTimeStyles.None, out dateTime10);
dateString = "not a date";
// Exception: The string was not recognized as a valid DateTime. There is an unknown word starting at index 0.
DateTime dateTime11; // 1/1/0001 12:00:00 AM
bool isSuccess2 = DateTime.TryParseExact(dateString, "MM/dd/yyyy", provider, DateTimeStyles.None, out dateTime11);
dateString = "Tue Dec 30, 2015";
DateTime dateTime12; //1/1/0001 12:00:00 AM
// Exception: String was not recognized as a valid DateTime because the day of week was incorrect.
bool isSuccess3 = DateTime.TryParseExact(dateString, "MM/dd/yyyy", provider, DateTimeStyles.None, out dateTime12);
dateString = "10-22-2015";
DateTime dateTime13; // 1/1/0001 12:00:00 AM
bool isSuccess4 = DateTime.TryParseExact(dateString, "MM/dd/yyyy", provider, DateTimeStyles.None, out dateTime13);
dateString = "10-22-2015";
DateTime dateTime15; // 10/22/2015 12:00:00 AM
bool isSuccess5 = DateTime.TryParseExact(dateString, "MM-dd-yyyy", provider, DateTimeStyles.None, out dateTime15);
dateString = "10-12-2015";
// Output: 10/22/2015 12:00:00 AM
DateTime dateTime14;
bool isSuccess6 = DateTime.TryParseExact(dateString, new string[]{ "MM/dd/yyyy", "MM-dd-yyyy", "MM.dd.yyyy"}, provider, DateTimeStyles.None, out dateTime14);
번역사이트
https://www.c-sharpcorner.com/UploadFile/manas1/string-to-datetime-conversion-in-C-Sharp/
String To DateTime Conversion In C#
C# convert string to datetime. Learn how to convert a string to datetime in C#. There are several common methods including Convert.ToDateTime, DateTime.Parse, and DateTime.ParseExact to convert a string into a DateTime in C#.
www.c-sharpcorner.com
C# : AWS SES 에서 첨부파일 attach files 을 발송 (MineKit 활용) (0) | 2023.04.17 |
---|---|
C# IQueryable과 IEnumerable 차이점 (0) | 2023.03.21 |
C# 의 Data Types (Data 타입) 알아보기 (0) | 2022.11.28 |
C# : 카카오맵의 API를 통해 주소 검색 및 위도 경도 검색하여 주소 얻기 (2) | 2022.11.18 |
기준날짜와 생년월일이 주어졌을 때 현재 나이 구하기 (0) | 2022.10.25 |
내 블로그 - 관리자 홈 전환 |
Q
Q
|
---|---|
새 글 쓰기 |
W
W
|
글 수정 (권한 있는 경우) |
E
E
|
---|---|
댓글 영역으로 이동 |
C
C
|
이 페이지의 URL 복사 |
S
S
|
---|---|
맨 위로 이동 |
T
T
|
티스토리 홈 이동 |
H
H
|
단축키 안내 |
Shift + /
⇧ + /
|
* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.