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 NSTimeInterval of X seconds.
I need to do it like this because the NSTimeInterval needs to be converted to an int by using lround to round up, then converted to a NSDate to use the NSDateFormatter to throw it into a string.
An
NSTimeInterval, as its name, um, implies, doesn’t represent the same thing as anNSDate. AnNSDateis 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
NSDaterepresenting 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):