2011-11-01 2 views
15

NSTimeIntervalNSDate으로 변환하려면 어떻게해야합니까? 스톱워치처럼 생각하십시오. 나는 처음 날짜를 00:00:00으로하고, NSTimeInterval을 X 초로한다. 나는 NSTimeInterval이 모아 lround를 사용하여 int로 변환 할 필요가 있기 때문에 이런 식으로 할 필요가NSDate에 NSTimeInterval

는 다음 문자열로 그것을 던져 NSDateFormatter를 사용하려면 NSDate로 변환.

답변

32

NSTimeInterval은 그 이름에서 암시하는 바와 같이 NSDate과 같은 것을 나타내지 않습니다. NSDate이며 시간은입니다. 시간 간격은 시간의 연장입니다. 일정한 간격에서 포인트를 얻으려면 다른 포인트가 있어야합니다. 귀하의 질문은 "나는이 보드에서 12 인치를 자리로 어떻게 변환합니까?"라고 묻는 것과 같습니다. 글쎄, 12 인치 시작 에서 어디?

참조 날짜를 선택해야합니다. 카운터를 시작한 시간을 나타내는 NSDate 일 가능성이 큽니다. 그럼 당신은 +[NSDate dateWithTimeInterval:sinceDate:] 또는 -[NSDate dateByAddingTimeInterval:]

을 사용할 수 있습니다. 그렇습니다. 나는 당신이 이것을 거꾸로 생각하고 있다고 확신합니다. 시작 지점 이후 경과 한 시간, 즉 간격 인을 현재 시간이 아닌 현재 시간으로 표시하려고합니다. 디스플레이를 업데이트 할 때마다 새 간격을 사용해야합니다. 예를 들어 (업데이트를 수행하기 위해 주기적으로 타이머 발사를 가정) :

- (void) updateElapsedTimeDisplay: (NSTimer *)tim { 

    // You could also have stored the start time using 
    // CFAbsoluteTimeGetCurrent() 
    NSTimeInterval elapsedTime = [startDate timeIntervalSinceNow]; 

    // Divide the interval by 3600 and keep the quotient and remainder 
    div_t h = div(elapsedTime, 3600); 
    int hours = h.quot; 
    // Divide the remainder by 60; the quotient is minutes, the remainder 
    // is seconds. 
    div_t m = div(h.rem, 60); 
    int minutes = m.quot; 
    int seconds = m.rem; 

    // If you want to get the individual digits of the units, use div again 
    // with a divisor of 10. 

    NSLog(@"%d:%d:%d", hours, minutes, seconds); 
} 
6

을 당신이 당신의 초기 날짜가 NSDate 객체에 저장되어있는 경우, 당신은 새로운 날짜를 미래에 어떤 간격을 얻을 수 있습니다. 간단하게이 같은 dateByAddingTimeInterval:을 사용

NSDate * originalDate = [NSDate date]; 
NSTimeInterval interval = 1; 
NSDate * futureDate = [originalDate dateByAddingTimeInterval:interval]; 
12

난 당신이 시간 간격을 표시하고자하는 경우 NSDateFormatter를 사용하여 무리를 줄 것이다. NSDateFormatter은 현지 시간대 또는 특정 시간대로 시간을 표시하고자 할 때 유용합니다. 그러나이 경우 시간대가 조정 된 경우 버그가 발생합니다 (예 : 1 년에 하루 23 시간).

다시
NSTimeInterval time = ...; 
NSString *string = [NSString stringWithFormat:@"%02li:%02li:%02li", 
               lround(floor(time/3600.)) % 100, 
               lround(floor(time/60.)) % 60, 
               lround(floor(time)) % 60]; 
+0

완벽하게! 내가 필요한 것! – hfossli

14

에서 쉽게 변환이 여기에 표시됩니다 :

NSDate * now = [NSDate date]; 
NSTimeInterval tiNow = [now timeIntervalSinceReferenceDate]; 
NSDate * newNow = [NSDate dateWithTimeIntervalSinceReferenceDate:tiNow]; 

올레 K에게 Hornnes

2

apple developers 통해 :

// 1408709486 - 시간 간격 값

NSDate *lastUpdate = [[NSDate alloc] initWithTimeIntervalSince1970:1408709486]; 

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
[dateFormatter setDateStyle:NSDateFormatterMediumStyle]; 
[dateFormatter setTimeStyle:NSDateFormatterMediumStyle]; 

NSLog(@"date time: %@", [dateFormatter stringFromDate:lastUpdate]); 

당신은 얻을 것이다 : 날짜 시간 : Aug 22, 2014, 3:11:26 PM

0

조쉬가 올바른 방법을 자세히 설명했듯이 간격을 NSDate 형식으로 유지하려면 다음 방법을 사용할 수 있습니다. 스위프트의

+ (NSDate *) dateForHour:(int) hour andMinute: (int) minute{ 
    NSDateComponents * components = [NSDateComponents new]; 
    components.hour = hour; 
    components.minute = minute; 
    NSDate * retDate = [[NSCalendar currentCalendar] dateFromComponents:components]; 
return retDate;} 
0

NSTimeIntervalNSDate 변환 :

let timeInterval = NSDate.timeIntervalSinceReferenceDate() // this is the time interval 
NSDate(timeIntervalSinceReferenceDate: timeInterval) 
관련 문제