NSDateFormatter Objective-c

Associate
Joined
15 Oct 2009
Posts
579
Ok i'm getting just a little peed off at the moment with this so im wondering if anyone here can point me in the right direction.

Im creating a countdown timer at the moment for an iPhone app and im using NSDateFormatter to parse a date from a string into a usable NSDate object to then compare it to the current date. I've gone though and followed all of the correct documents from apple themselves and it works fine on the simulator, but when it comes to the device it just doesn't work at all.

Code:
NSDate *dateNow = [NSDate date];
	
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC+2"]];
	
NSDate *endDate = [dateFormatter dateFromString:[NSString stringWithFormat:@"2010-06-11 16:00"]];
	
NSTimeInterval interval = [endDate timeIntervalSinceDate:dateNow];
	
if ([dateNow compare:endDate] == NSOrderedDescending) 
{
	//Stop Timer
        [timerA release];
	[self checkExpiry];
}

I've been through the documents from apple which can be found here: http://img101.imageshack.us/img101/883/screenshot20100415at180.png and checked the formatting which can be found here: http://unicode.org/reports/tr35/tr35-4.html#Date_Format_Patterns but im still having no luck.

The error im getting from the NSDate when i try to assign the NSDateFormatter is <invalid CFStringRef>.

I asked another forum dedicated to iPhone development but they wern't able to give me an answer either, so im hoping the clever people here can point me in the right direction instead ;)
 
Ahh this looks just tup mah street :)

Firstly - the simulator will allow you to get away with classes/methods not actually available on the iPhone itself (if you start network coding you'll find that out too).
iPhone OS Note: iPhone OS supports only the modern 10.4+ behavior. 10.0-style methods and format strings are not available on iPhone OS.

As you use "init" this will switch it to 10.4+ strings so you don't need to explictly set the behaviour.

Give me a few mins.. although I tend to just drag the formatter objects into the fields in the xib..
 
Bah.. editing went foobar there - from the device itself:

Thu Apr 15 21:09:59 ... test[184] <Warning>: date = 2010-06-11 16:00:00 +0100

That's just running:
Code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    
	NSDate *dateNow = [NSDate date];
	
	NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
	[dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
	[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
	[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC+2"]];
	
	NSDate *endDate = [dateFormatter dateFromString:[NSString stringWithFormat:@"2010-06-11 16:00"]];
	
	NSLog(@" date = %@", endDate);
	
	NSTimeInterval interval = [endDate timeIntervalSinceDate:dateNow];
	
	if ([dateNow compare:endDate] == NSOrderedDescending) 
	{
		//Stop Timer
		//[timerA release];
		//[self checkExpiry];
	}
	
	
    // Override point for customization after app launch    
	return YES;
}

There could be a code fix that you may not have - what version of Xcode & device are you running against?
 
Last edited:
Im running xCode 3.2.2 and using the 3.1.2 version of the firmware, I do have my iPhone jailbroken too but i can't see that really being an issue.

It's nice to know that im not going mad and the code does work as it should though, I was starting to worry!

The only reason why i explicitly evoked the 10.4 behavior was because i was running out of options and was just trying anything and everything i could to get it working.

Thanks for having a look at this, i was hoping it was you who picked up on this thread :D
 
Back
Top Bottom