반응형
String 날짜를 NSDate로 변경 후 NSString 포맷형식에 맞게 변환하기
// 표시할 날짜를 만든다.
NSString *str = [NSString stringWithFormat:@"%@ %@", @"11.29", @"12:23"];
// 날짜의 형식을 변경하기위한 NSDateFormatter 객체를 생성
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// 여기 출력 날짜에있는 날짜 형식을 설정(형식이 str보다 위에 있음을 의미)
[dateFormatter setDateFormat:@"MM.dd HH:mm"];
// 여기에서 정의 형식으로 문자열에서 날짜를 가져올 수 았다
NSDate *date = [dateFormatter dateFromString: str];
dateFormatter = [[NSDateFormatter alloc] init];
// 원하는 형식을 설정 ..
[dateFormatter setDateFormat:@"MM.dd(HH:mm)"];
// NSString에서 날짜를 변환
NSString *convertedString = [dateFormatter stringFromDate:date];
반응형