Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 08-30-2009, 12:05 AM   #1 (permalink)
Registered Member
 
Join Date: Aug 2009
Location: US
Posts: 31
Lightbulb NSString to NSDate

I got a string that contains the current date by using this :

Code:
NSString *date = [[NSDate date] description];
At a different point I am want to retrieve the date from this string and I use the following code
Code:
[NSDateFormatter setDefaultFormatterBehavior:NSDateFormatterBehavior10_4];
	NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
	//[NSDateFormatter setDefaultFormatterBehavior:NSDateFormatterBehaviorDefault];
	[dateFormatter setDateFormat:@"YYYY-MM-DD HH:MM:SS ±HHMM"];

	NSDate *dateFromString = [[NSDate alloc] init];
	dateFromString = [dateFormatter dateFromString:<NSString containing date>];
I am getting 'dateFromString' as nil 0x0. What I am doing wrong?
shad1236 is offline   Reply With Quote
Old 08-30-2009, 10:18 AM   #2 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,129
Default

What does the string look like before you do that?
__________________
BriTer Ideas LLC - Code review, consulting, development. PM for pricing.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
BrianSlick is offline   Reply With Quote
Old 08-30-2009, 03:54 PM   #3 (permalink)
Registered Member
 
Join Date: Aug 2009
Location: US
Posts: 31
Default

The string was in this format:

NSString *dateString = @"2009-08-30 07:47:52 -400";
shad1236 is offline   Reply With Quote
Old 08-30-2009, 04:25 PM   #4 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,129
Default

I changed it to this:

Code:
[dateFormatter setDateFormat:@"yyyy-mm-dd hh:mm:ss"];
and chopped the appropriate bit off the string, and it worked just fine. From a little research, it seems that if the format is not correct, the return from the dateFromString method is nil. So it seems that your format is not correct.
__________________
BriTer Ideas LLC - Code review, consulting, development. PM for pricing.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
BrianSlick is offline   Reply With Quote
Old 08-30-2009, 06:41 PM   #5 (permalink)
Registered Member
 
Join Date: Aug 2009
Location: US
Posts: 31
Default

i used this one
Code:
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss ZZZZ"];
Thanks BrianSlick for your help
shad1236 is offline   Reply With Quote
Reply

Bookmarks

Tags
date, nsdate, nsdateformatter, nsstring, string

Thread Tools
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
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 251
24 members and 227 guests
@sandris, ADY, Dani77, diyora, FAED, fredidf, F_Bryant, GHuebner, HDshot, iDifferent, JasonR, mer10, Oral B, prchn4christ, Rudy, smithdale87, Speed, spiderguy84, stekki, tgjorgoski, Thompson22, Touchmint, twerner, vigu360
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,880
Threads: 89,228
Posts: 380,752
Top Poster: BrianSlick (7,129)
Welcome to our newest member, @sandris
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 01:04 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0