Ready to Level-Up?

This just in! If your rusty iOS skills could use some refining, or if you're just starting out and you want to level-up fast, consider signing up for our upcoming iOS Screencasts

New from iOS Rocket Surgery Swift Video Tutorials: The rapidly evolving Apple-iOS ecosystem has just been thrown into tumult with the recent introduction of the Swift Programming Language. Are you ready to finally try iOS development? Are you needing to switch from Objective-C to Swift? Sign up today for our free video courses!
(By the dear folks at Swift Tutorial Videos, a sister company)

Monday, April 14, 2014

iOS NSDate Minus 1 Hour

So, you want to subtract an hour from an NSDate, huh?

If you're coming from another language like Java or Ruby, you're probably pretty familiar with the idea of creating a date by adding or subtracting some time from a Date object. The equivalent function in iOS is as follows:


So, for example, if you wanted to get a date representing one hour from now (60 seconds/minute * 60 minutes/hour), you would do as follows:

NSDate *anHourFromNow = [[NSDate alloc] dateByAddingTimeInterval:60*60];

If you wanted to do an hour ago, you'd add a negative value like so:
NSDate *anHourBeforeNow = [[NSDate alloc] dateByAddingTimeInterval:-60*60];

Thats it! So easy once you know the method calls.

If you like this, or found it helpful, or copied our code to use in your project :). A quick tweet would be awfully nice of you, but we'd settle for a +1 on Google Plus!

About Me

Easy NSDateFormatter Tool

Save yourself some time in formatting your NSDates to NSStrings, and use the Blind NSDate app, which you can download from iTunes. There's also a website where you can format your NSDates:

Popular Posts

Designed By Seo Blogger Templates