views:

740

answers:

3

I have an NSDate and a duration. I need to get the time after the duration...

Given: NSDate is 2010-02-24 12:30:00 -1000 duration is 3600 secs

I need to get 2010-02-24 13:30:00 -1000

Actually I am looking for the time, but I think I know how to do that if I can get the NSDate object reflecting the new time.

I thought dateWithTimeIntervalSinceReferenceDate would do the trick but I see now that this gives date from 1 jan 2001 gmt.

Is this another c function I need to use?

Thanks for any help.

John

+3  A: 

You want the class method dateWithTimeInterval:sinceDate: which takes the starting date and the interval NSDate docs

Mark
I cannot find dateWithTimeInterval:sinceDate, but now that you made me look again I do see initWithTimeInterval:sinceDate:. That did the trick. Thanks.
`dateWithTimeInterval:sinceDate` is a class method, rather than an instance method, that returns the result as an autoreleased NSDate. You get used to the naming conventions eventually.
Abizern
OK, I see why I don't see dateWithTimeIntervalSinceDate. Your link is to the Mac OSX Reference Library. It is not available for the iPhone.But as others have pointed out there are several other ways to do it that are available on the iPhone.
With your tags mentioning Cocoa instead of Cocoa-touch, and there being nothing about iPhone, it's not surprising that the answers you get are based on the Desktop SDK. I've corrected the tags for you.
Abizern
+3  A: 

You can use the addTimeInterval instance method of NSDate:

NSDate *newDate = [oldDate addTimeInterval:3600];

Edit: As Chip Coons correctly points out, this method has been deprecated. Please use one of the other methods instead.

DyingCactus
+6  A: 

As DyingCactus states, you can use the addTimeInterval method of NSDate, but depending on the OS version is will create a compiler warning, since it is deprecated in 10.6 as of 2009-08-17.

The current recommended method is to use dateByAddingTimeInterval on NSDate:

NSDate *newDate = [oldDate dateByAddingTimeInterval:3600];

Note that if you are targeting 10.5 or earlier, the original answer will work.

Chip Coons
Thanks to all who answered. I did not read the docs well enough on this one.