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)

Saturday, September 28, 2013

Simple iOS NSDate Format Example


So, you want to convert an NSDate to NSString, eh?

No problem. With the help of our trusty NSDateFormatter class, we can turn any date into a string with a few keystrokes.

// Get today's date
NSDate *myDate = [NSDate date];
  
// Create an iOS date formatter and set the pattern
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
  
// Convert the NSDate to NSString
NSString *dateString = [formatter stringFromDate:myDate];

Wow, that was easy! This is a fun trick that comes in handy all the time, especially when you want to display a nicely formatted timestamp, or when you want to send date information over the wire to your API endpoints.






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: http://www.blindnsdate.com

Popular Posts

Designed By Seo Blogger Templates