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

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

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

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.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 01-13-2010, 09:01 PM   #1 (permalink)
A Young Perfectionist
 
atothendrew's Avatar
 
Join Date: Jun 2009
Posts: 111
atothendrew is on a distinguished road
Send a message via AIM to atothendrew
Default Convert NSString to NSDate

I have the following code to convert an NSDate to an NSString, but the date keeps coming back as nil. Any ideas as to why?

Code:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
	[dateFormat setDateFormat:@"HH:mm a"];
	NSDate *intime = [dateFormat dateFromString:inTime_];
I need everything in the NSDate, even the AM/PM. Thats why i have the 'a' after HH:mm. Thanks guys
__________________
Work Progress - The Perfect app for all!

The Advanced Random Number Generator

FPS Stats - Avaliable now!
The users are LOVING it! and new features are added Weekly!
"This is by far the greatest app I have ever used. Great Idea!" - KyRo .LV, 5 stars

atothendrew is offline   Reply With Quote
Old 01-13-2010, 09:06 PM   #2 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

nil return usually means that your string doesn't match the format. What is the string you are passing in?
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

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

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 01-13-2010, 09:08 PM   #3 (permalink)
A Young Perfectionist
 
atothendrew's Avatar
 
Join Date: Jun 2009
Posts: 111
atothendrew is on a distinguished road
Send a message via AIM to atothendrew
Default

im passing in a string called 'inTime_' I ran a debugger and its value is '7:55 PM'

Thanks for your help
__________________
Work Progress - The Perfect app for all!

The Advanced Random Number Generator

FPS Stats - Avaliable now!
The users are LOVING it! and new features are added Weekly!
"This is by far the greatest app I have ever used. Great Idea!" - KyRo .LV, 5 stars

atothendrew is offline   Reply With Quote
Old 01-13-2010, 09:11 PM   #4 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

UTS #35: Locale Data Markup Language

Looks like H is for 24-hour clock, so AM/PM wouldn't apply. Not sure if that's the issue or not. Try h or hh.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

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

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 01-13-2010, 09:14 PM   #5 (permalink)
A Young Perfectionist
 
atothendrew's Avatar
 
Join Date: Jun 2009
Posts: 111
atothendrew is on a distinguished road
Send a message via AIM to atothendrew
Default

Thanks for the reply. I checked that website and it seems like 'h:mm a' is what i need. But for some reason it still does not work.. Weird
__________________
Work Progress - The Perfect app for all!

The Advanced Random Number Generator

FPS Stats - Avaliable now!
The users are LOVING it! and new features are added Weekly!
"This is by far the greatest app I have ever used. Great Idea!" - KyRo .LV, 5 stars

atothendrew is offline   Reply With Quote
Old 01-13-2010, 09:16 PM   #6 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

Add this:
Code:
NSLog(@"inTime_ is: %@", inTime_);
See what it says.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

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

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 01-13-2010, 09:18 PM   #7 (permalink)
A Young Perfectionist
 
atothendrew's Avatar
 
Join Date: Jun 2009
Posts: 111
atothendrew is on a distinguished road
Send a message via AIM to atothendrew
Default

i get the following output : inTime_ is: 8:15 PM
__________________
Work Progress - The Perfect app for all!

The Advanced Random Number Generator

FPS Stats - Avaliable now!
The users are LOVING it! and new features are added Weekly!
"This is by far the greatest app I have ever used. Great Idea!" - KyRo .LV, 5 stars

atothendrew is offline   Reply With Quote
Old 01-13-2010, 09:21 PM   #8 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

Ok, then this:
Code:
NSLog(@"intime is: %@", intime);
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

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

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 01-13-2010, 09:28 PM   #9 (permalink)
A Young Perfectionist
 
atothendrew's Avatar
 
Join Date: Jun 2009
Posts: 111
atothendrew is on a distinguished road
Send a message via AIM to atothendrew
Default

Wow. This is REALLY weird. the output is 'intime is: 1970-01-01 20:26:00 -0700'

i dont even know where to go from here.. im so confused
__________________
Work Progress - The Perfect app for all!

The Advanced Random Number Generator

FPS Stats - Avaliable now!
The users are LOVING it! and new features are added Weekly!
"This is by far the greatest app I have ever used. Great Idea!" - KyRo .LV, 5 stars

atothendrew is offline   Reply With Quote
Old 01-13-2010, 09:28 PM   #10 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

How did you know it was nil in the first post?
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

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

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 01-13-2010, 09:29 PM   #11 (permalink)
A Young Perfectionist
 
atothendrew's Avatar
 
Join Date: Jun 2009
Posts: 111
atothendrew is on a distinguished road
Send a message via AIM to atothendrew
Default

Quote:
Originally Posted by BrianSlick View Post
How did you know it was nil in the first post?
i debugged and dragged my mouse over the variable name and the pop up told me it was nil.
__________________
Work Progress - The Perfect app for all!

The Advanced Random Number Generator

FPS Stats - Avaliable now!
The users are LOVING it! and new features are added Weekly!
"This is by far the greatest app I have ever used. Great Idea!" - KyRo .LV, 5 stars


Last edited by atothendrew; 01-13-2010 at 09:32 PM.
atothendrew is offline   Reply With Quote
Old 01-13-2010, 09:31 PM   #12 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

So is something downstream not working?

I don't use the debugger very much, but I suppose it's possible you were getting the value of the variable before the dateFromString happened.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

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

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 01-13-2010, 09:33 PM   #13 (permalink)
A Young Perfectionist
 
atothendrew's Avatar
 
Join Date: Jun 2009
Posts: 111
atothendrew is on a distinguished road
Send a message via AIM to atothendrew
Default

This does not make any sense.. i formatted it the way i needed too..

its supposed to come up as 'Hour:Minute PM'
__________________
Work Progress - The Perfect app for all!

The Advanced Random Number Generator

FPS Stats - Avaliable now!
The users are LOVING it! and new features are added Weekly!
"This is by far the greatest app I have ever used. Great Idea!" - KyRo .LV, 5 stars

atothendrew is offline   Reply With Quote
Old 01-13-2010, 09:34 PM   #14 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

What? What are you expecting from an NSDate object?
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

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

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 01-13-2010, 09:36 PM   #15 (permalink)
A Young Perfectionist
 
atothendrew's Avatar
 
Join Date: Jun 2009
Posts: 111
atothendrew is on a distinguished road
Send a message via AIM to atothendrew
Default

Quote:
Originally Posted by BrianSlick View Post
What? What are you expecting from an NSDate object?
well quite honestly i was going to convert this string "h:mm a" <-- what ever that would be, the user enters, into an nsdate and then convert that the hours and minutes into doubles.
__________________
Work Progress - The Perfect app for all!

The Advanced Random Number Generator

FPS Stats - Avaliable now!
The users are LOVING it! and new features are added Weekly!
"This is by far the greatest app I have ever used. Great Idea!" - KyRo .LV, 5 stars

atothendrew is offline   Reply With Quote
Old 01-13-2010, 09:40 PM   #16 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

Well, you have the date object now. For getting your doubles, look into NSDateComponents.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

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

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 01-13-2010, 09:52 PM   #17 (permalink)
A Young Perfectionist
 
atothendrew's Avatar
 
Join Date: Jun 2009
Posts: 111
atothendrew is on a distinguished road
Send a message via AIM to atothendrew
Default

sweet. Thanks alot for your help
__________________
Work Progress - The Perfect app for all!

The Advanced Random Number Generator

FPS Stats - Avaliable now!
The users are LOVING it! and new features are added Weekly!
"This is by far the greatest app I have ever used. Great Idea!" - KyRo .LV, 5 stars

atothendrew is offline   Reply With Quote
Old 01-13-2010, 09:56 PM   #18 (permalink)
A Young Perfectionist
 
atothendrew's Avatar
 
Join Date: Jun 2009
Posts: 111
atothendrew is on a distinguished road
Send a message via AIM to atothendrew
Default

Quote:
Originally Posted by BrianSlick View Post
What? What are you expecting from an NSDate object?
One last thing. I looked into it and found what i needed.

Code:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
	[dateFormat setDateStyle:NSDateFormatterNoStyle];
	[dateFormat setTimeStyle:NSDateFormatterShortStyle];
	[dateFormat setDateFormat:@"h:mm a"];
	NSDate *intime = [dateFormat dateFromString:inTime_];   
	NSLog(@"intime is: %@", intime);
	NSLog(@"inTime_ is: %@", inTime_);
	
	NSCalendar *calendar= [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
	NSCalendarUnit unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
	NSDateComponents *dateComponents = [calendar components:unitFlags fromDate:intime];
	
    
	
	double inHour = [dateComponents hour];
	double inMin = [dateComponents minute];
	
	NSLog(@"inTime Hours is: %@", inHour);
That last NSLog = inTime Hours is: (null)

I did everything like how i did in other programs. Man this thing is a headache.
__________________
Work Progress - The Perfect app for all!

The Advanced Random Number Generator

FPS Stats - Avaliable now!
The users are LOVING it! and new features are added Weekly!
"This is by far the greatest app I have ever used. Great Idea!" - KyRo .LV, 5 stars

atothendrew is offline   Reply With Quote
Old 01-13-2010, 09:58 PM   #19 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

%@ is for objects. You want %f.

And really, those could be integers. In which case, %d.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

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

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 01-13-2010, 10:00 PM   #20 (permalink)
A Young Perfectionist
 
atothendrew's Avatar
 
Join Date: Jun 2009
Posts: 111
atothendrew is on a distinguished road
Send a message via AIM to atothendrew
Default

Quote:
Originally Posted by BrianSlick View Post
%@ is for objects. You want %f.

And really, those could be integers. In which case, %d.
Of course.. I don't know what i was thinking. I'm exhausted. Thanks alot again. You rock. Ill definitely check out your app!
__________________
Work Progress - The Perfect app for all!

The Advanced Random Number Generator

FPS Stats - Avaliable now!
The users are LOVING it! and new features are added Weekly!
"This is by far the greatest app I have ever used. Great Idea!" - KyRo .LV, 5 stars

atothendrew is offline   Reply With Quote
Reply

Bookmarks

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: 330
9 members and 321 guests
2Apps1Day, akacaj, Domele, michelle, NSString, SLIC, soohyun, Techgirl-52, v1n2e7t
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,650
Threads: 94,114
Posts: 402,886
Top Poster: BrianSlick (7,990)
Welcome to our newest member, soohyun
Powered by vBadvanced CMPS v3.1.0

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