본문 바로가기

프로그래밍/iOS

[iOS] 특정날짜를 NSDate로 변환하는 함수

원하는 날짜를 NSDate로 바꾸는 함수


- 사용법

[self getNSDateFromYear:2016 withMonth:3 withDay:16 withHour:9 withMin:41 withSecond:0];

- 함수

- (NSDate*)getNSDateFromYear:(int)year withMonth:(int)mon withDay:(int)day withHour:(int)hour withMin:(int)min withSecond:(int)second

{

    NSCalendar *calendar = [NSCalendar currentCalendar];

    NSDateComponents *components = [[NSDateComponents alloc] init];

    [components setYear:year];

    [components setMonth:mon];

    [components setDay:day];

    [components setHour:hour];

    [components setMinute:min];

    [components setSecond:second];

    return [calendar dateFromComponents:components];

}