Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
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 601
 
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 offline   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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 08:37 AM.

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

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