2013-03-23 2 views
0

날짜 선택 도구가 있습니다. 이 시간을 선택하면 다음 30 분과 1 시간의 날짜와 시간을 얻고 싶습니다.미래 시간을 NSDate로 가져 오기

날짜와 시간을 가져 와서 다음 30 분과 1 시간 동안 두 NSDate로 반환하는 방법을 쓰려면 어떻게해야할까요?

예 : 날짜 선택 도구에서 오후 1시 30 분을 선택한 다음 다음 시간을 가져오고 다음 30 분 시간과 1 시간 시간을 가져 오려고합니다.

누구에게도 도움이 될 수 있습니까 ??

+0

사용'NSDateComponents'. –

+0

NSDateComponents는 고정 된 길이가 없기 때문에 요일이나 달과 같은 간격을 추가 할 때 유용합니다. 30 분을 추가하려면'dateByAddingTimeInterval'으로 충분해야합니다. –

답변

1

의 dateByAddingTimeInterval 메소드를 사용할 수 있습니다.

예 :

NSDate *NextDateOneHour = [oldDate dateByAddingTimeInterval:3600]; //3600 second equivalent to 60 mins 
NSDate *NextDateHalfAnHour = [oldDate dateByAddingTimeInterval:1800]; //1800 second equivalent to 30 mins 
0
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
NSDateComponents *comps = [[NSDateComponents alloc] init]; 
[comps setHour:1]; 

NSDate *afterHour = [gregorian dateByAddingComponents:comps toDate:[NSDate date] options:0]; 

[comps setMinute:30]; 

NSDate *afterHalfHour = [gregorian dateByAddingComponents:comps toDate:[NSDate date] options:0]; 
2
NSCalendar *calendar = [NSCalendar currentCalendar]; 
    NSDateComponents *components = [NSDateComponents new]; 
    components.minute = 30; 
    components.hour = 1; 
    NSDate *date = [calendar dateByAddingComponents:components toDate:< your date > options:0];