본문 바로가기

블로그/아이폰/iOS

NSDate를 NSString으로, NSString을 NSDate로 변환하는 방법.

#pragma mark - NSDate <->NSString

-(NSDate *)getDateFromString:(NSString *)string

{

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

    [dateFormatter setDateFormat:@"yyyy-MM-dd-HH Z"];

    NSDate *date = [dateFormatter dateFromString:string];

    return date;

}


-(NSString*)getStringFromDate:(NSDate*)date

{

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

    [dateFormatter setDateFormat:@"yyyy MM dd HH"];

    

    NSString *string = [dateFormatter stringFromDate:date];

    

    return string;

}