James
James

Reputation: 331

Problems converting a NSDate to a string

This extremely simple task was apparantly very hard...

NSDate *date;
date = [someMethod here];
//I've checked with debugger that the method returns an object of type NSDate. 
//The description of date at this point is: "2012-02-02 19:42:00 +0000"

NSDateFormatter *dateFormat;
[dateFormat setDateFormat:@"dd-MM-yy hh:mm"];
NSString *dateString = [dateFormat stringFromDate:date];

dateString is just NIL

Does anyone know what's wrong?

Edit: What i really want to achieve is simply:

NSString *receivedDate = @"2012-02-02T20:42:00+01:00";
NSString *fixedDate = [do some magic]
//value of fixedDate is now: "02-02-12 20:42"

Upvotes: 0

Views: 315

Answers (1)

peterept
peterept

Reputation: 4427

As Anna said you need to allocate an instance of NSDateFormatter because all you get is a NULL ptr, which just ignores the messages setDateFormat and stringFromDate, leaving you with NULL.

But also your format is for hours is incorrect. Refer to Date Formatter reference

This works for me:

NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
[df setDateFormat:@@"dd-MM-yy HH:mm"];
NSString *dateString = [df stringFromDate:date];

Upvotes: 3

Related Questions