Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jan 22, 2013, 05:27 AM   #1
mikezang
macrumors 6502a
 
Join Date: May 2010
Location: Tokyo, Japan
Why can't convert NSString to NSDate?

I have a string is @"Tuesday, January 01, 2013", and I use code as below to convert it to date, but I only get nil, does anyone tell me why?
Code:
+ (NSDate *)dateFromString:(NSString *)string withFormat:(NSString *)format {
	NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
	[inputFormatter setDateFormat:format];
	NSDate *date = [inputFormatter dateFromString:string];
	return date;
}

NSString *str = @"Tuesday, January 01, 2013";
NSDate *date = [NSDate dateFromString:str withFormat:@"EEEE, MMMM dd, yyyy"];
__________________
iPad Mini 32GB, iPhone 4S 16GB, iPad1 32GB, Mac mini 2.53GHz 4GB 320GB, ThinkPad T60
MyApp: Market Event v1.9.2
mikezang is offline   0 Reply With Quote
Old Jan 22, 2013, 05:30 AM   #2
Mvkoe
macrumors member
 
Join Date: Aug 2008
Location: Belgium
Quote:
Originally Posted by mikezang View Post
I have a string is @"Tuesday, January 01, 2013", and I use code as below to convert it to date, but I only get nil, does anyone tell me why?
Code:
+ (NSDate *)dateFromString:(NSString *)string withFormat:(NSString *)format {
	NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
	[inputFormatter setDateFormat:format];
	NSDate *date = [inputFormatter dateFromString:string];
	return date;
}

NSString *str = @"Tuesday, January 01, 2013";
NSDate *date = [NSDate dateFromString:str withFormat:@"EEEE, MMMM dd, yyyy"];
http://stackoverflow.com/questions/1...te-to-nsstring ?
Mvkoe is offline   0 Reply With Quote
Old Jan 22, 2013, 08:08 AM   #3
mikezang
Thread Starter
macrumors 6502a
 
Join Date: May 2010
Location: Tokyo, Japan
Quote:
Originally Posted by Mvkoe View Post
Doesn't work!
__________________
iPad Mini 32GB, iPhone 4S 16GB, iPad1 32GB, Mac mini 2.53GHz 4GB 320GB, ThinkPad T60
MyApp: Market Event v1.9.2
mikezang is offline   0 Reply With Quote
Old Jan 22, 2013, 09:19 AM   #4
ArtOfWarfare
macrumors 603
 
ArtOfWarfare's Avatar
 
Join Date: Nov 2007
Send a message via Skype™ to ArtOfWarfare
Where is your class method declared? Have you added a category for NSDate?

Edit: my prior edit was incorrect. But have a look here, the author has an example of what you want near the bottom:

http://www.cocoawithlove.com/2009/05...g-and.html?m=1

Last edited by dejo; Jan 22, 2013 at 10:37 AM. Reason: Fixed link.
ArtOfWarfare is online now   0 Reply With Quote
Old Jan 22, 2013, 06:56 PM   #5
mikezang
Thread Starter
macrumors 6502a
 
Join Date: May 2010
Location: Tokyo, Japan
Sure, I put that method in category NSDate+Helper
mikezang is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Resolved: NSDate singleton. IDMah iPhone/iPad Programming 3 Mar 18, 2014 11:16 AM
sorting an array of NSdate daproject85 iPhone/iPad Programming 5 Sep 15, 2012 08:58 AM
NSDate Update? xArtx iPhone/iPad Programming 4 Jul 22, 2012 07:42 AM
(NSString*) ?? thedon1 iPhone/iPad Programming 6 Jul 2, 2012 07:52 PM
iOS 5 - Core Data TableView Sections based on NSDate Ranges mdhansen5 iPhone/iPad Programming 1 Jun 13, 2012 01:16 AM

Forum Jump

All times are GMT -5. The time now is 09:52 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC