How can I convert a NSTimeInterval
to NSDate
? Think of it like a stopwatch. I want the initial date to be 00:00:00, and I have a NSTimeInterv
An NSTimeInterval
, as its name, um, implies, doesn't represent the same thing as an NSDate
. An NSDate
is a moment in time. A time interval is a stretch of time. To get a point from an interval, you have to have another point. Your question is like asking "How do I convert 12 inches to a spot on this board I'm cutting?" Well, 12 inches, starting from where?
You need to pick a reference date. This will most likely be the NSDate
representing the time that you started your counter. Then you can use +[NSDate dateWithTimeInterval:sinceDate:] or -[NSDate dateByAddingTimeInterval:]
That said, I'm pretty sure you're thinking about this backwards. You're trying to display time elapsed since some starting point, i.e., the interval, not the current time. Every time you update the display, you should just be using the new interval. For example (assuming you have a timer firing periodically to do the update):
- (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);
}
NSTimeInterval to NSDate conversion in Swift:
let timeInterval = NSDate.timeIntervalSinceReferenceDate() // this is the time interval
NSDate(timeIntervalSinceReferenceDate: timeInterval)
As Josh answer detailed the correct way, if you still want the interval to be in the NSDate format you can use the following method
+ (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;}
If you have your initial date stored in an NSDate
object, you can get a new date any interval in the future. Simply use dateByAddingTimeInterval:
like this:
NSDate * originalDate = [NSDate date];
NSTimeInterval interval = 1;
NSDate * futureDate = [originalDate dateByAddingTimeInterval:interval];
I would advise against using NSDateFormatter
if you wish to display time intervals. NSDateFormatter
is useful when you wish to display times in the local, or a specific, timezone. But in this case it would be a bug if the time was timezone adjusted (e.g. one day per year has 23 hours).
NSTimeInterval time = ...;
NSString *string = [NSString stringWithFormat:@"%02li:%02li:%02li",
lround(floor(time / 3600.)) % 100,
lround(floor(time / 60.)) % 60,
lround(floor(time)) % 60];
An easy conversion from and back is shown here:
NSDate * now = [NSDate date];
NSTimeInterval tiNow = [now timeIntervalSinceReferenceDate];
NSDate * newNow = [NSDate dateWithTimeIntervalSinceReferenceDate:tiNow];
Ole K Hornnes