views:

2943

answers:

1

I've got a NSDate that represents a specific time. The format of that date is hhmmss. I want to add an NSInterval value (specified in seconds) to that time value.

Example:

NSDate = 123000 NSTimeInterval = 3600

Added together = 133000

What is the best way to do this?

Thanks.

+4  A: 

Have you seen the Dates and Times Programming Topics for Cocoa manual?

Basically you need to convert your time into an NSDate. To convert a string to a date you use the NSDateFormatter class or maybe NSDateComponents if you already know the hours, minutes and seconds.

Your NSTimeInterval is just a double (i.e., 3600.0).

Then you use the addTimeInterval method of NSDate to add the number of seconds to the time.

NSDate* newDate = [oldDate addTimeInterval:3600.0];

You can then use either NSDateFormatter or NSDateComponents to get the new time back out again.

Stephen Darlington
Thanks, exactly what I needed!