0

I'm having some problems getting an NSString to convert to an NSDate.

Here is my code:

NSLog(@"Unformatted date: %@", [d objectForKey:@"pubDate"]);

// Set the publication date as an NSDate
static NSDateFormatter *dateFormatter = nil;
if (!dateFormatter) {
    dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"EEE, dd MMM yyyy"];
}
[self setPublicationDate:[dateFormatter dateFromString:[d objectForKey:@"pubDate"]]];

NSLog(@"Date object: %@", [self publicationDate]);
NSLog(@"Date object description: %@", [[self publicationDate] description]);

When this is run, it logs these messages (it is run several times, I've included just two sets of its log messages):

2012-08-14 12:47:28.446 (App name removed)[9009:707] Unformatted date: Mon, 02 Apr 2012
2012-08-14 12:47:28.454 (App name removed)[9009:707] Date object: (null)
2012-08-14 12:47:28.460 (App name removed)[9009:707] Date object description: (null)
2012-08-14 12:47:28.467 (App name removed)[9009:707] Unformatted date: Sun, 25 Mar 2012
2012-08-14 12:47:28.474 (App name removed)[9009:707] Date object: (null)
2012-08-14 12:47:28.480 (App name removed)[9009:707] Date object description: (null)

I'm testing this code on an iPad touch running iOS 5.1.1

I am able to change the format of the date being received, it is currently being sent from a PHP date function with "D, d M Y" being used as the format string.

Can anyone please see where I am going wrong? Thank you for your help.

UPDATE 1: I've switched over and ran it on the simulator instead of on a device and for some reason it's worked. Any suggestions as to why it isn't working on the device? Simulator is running iOS 5.1 and the device is running iOS 5.1.1

UPDATE 2: Some people have requested more information, publicationDate is a synthesised property.

In the .h file:

@property (nonatomic, strong) NSDate *publicationDate;

In the .m file:

@synthesize publicationDate;

I've broken the code up even further:

NSLog(@"Unformatted date: %@", [d objectForKey:@"pubDate"]);
NSLog(@"Unformatted date class type: %@", [[d objectForKey:@"pubDate"] class]);

// Set the publication date as an NSDate
static NSDateFormatter *dateFormatter = nil;
if (!dateFormatter) {
    dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"EEE, dd MMM yyyy"];
}
NSString *dateString = [d objectForKey:@"pubDate"];
NSLog(@"dateString: %@", dateString);

NSDate *formattedDate = [dateFormatter dateFromString:dateString];
NSLog(@"formattedDate %@", formattedDate);
[self setPublicationDate:formattedDate];

NSLog(@"Date object: %@", [self publicationDate]);
NSLog(@"Date object description: %@", [[self publicationDate] description]);

and it now logs:

2012-08-14 13:45:35.335 (app name removed)[9167:707] Unformatted date: Sun, 25 Mar 2012
2012-08-14 13:45:35.342 (app name removed)[9167:707] Unformatted date class type: __NSCFString
2012-08-14 13:45:35.347 (app name removed)[9167:707] dateString: Sun, 25 Mar 2012
2012-08-14 13:45:35.353 (app name removed)[9167:707] formattedDate (null)
2012-08-14 13:45:35.359 (app name removed)[9167:707] Date object: (null)
2012-08-14 13:45:35.365 (app name removed)[9167:707] Date object description: (null)

UPDATE 3: Success! By changing the date formatter like so:

// Set the publication date as an NSDate
static NSDateFormatter *dateFormatter = nil;
if (!dateFormatter) {
    dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
    [dateFormatter setDateFormat:@"EEE, dd MMM yyyy"];
}

It now works. I didn't think to mention that the device being tested on is set to Dutch settings (as this is a Dutch development company). Apologies, it never occurred to me that this might effect how the code here worked.

Edd Slipszenko
  • 396
  • 3
  • 17
  • why are you checking if(!dateFormatter) ? One line before is the dateformatter always set to nil? – NDY Aug 14 '12 at 11:16
  • 1
    @Andy you're wrong, only first time dateformatter == nil, because dateformatter is static variable. – tikhop Aug 14 '12 at 11:18
  • Because it's a static variable, the line of code that initiates the variable is only ever run once. – Edd Slipszenko Aug 14 '12 at 11:18
  • show your setPublicationDate method – tikhop Aug 14 '12 at 11:20
  • It's synthesised from this: `@property (nonatomic, strong) NSDate *publicationDate;` and `@synthesize publicationDate;`. – Edd Slipszenko Aug 14 '12 at 11:24
  • try to logging [dateFormatter dateFromString:[d objectForKey:@"pubDate"]] – tikhop Aug 14 '12 at 11:29
  • I've switched over and ran it on the simulator instead of on a device and for some reason it's worked. Any suggestions as to why it isn't working on the device? The simulator is running iOS 5.1 and the device is running iOS 5.1.1 – Edd Slipszenko Aug 14 '12 at 11:34
  • Check the answer for a very similar question: [Get NSDate from NSDate adjusted with timezone](http://stackoverflow.com/questions/7485493/get-nsdate-from-nsdate-adjusted-with-timezone/11887312#11887312) – Carlos Morales Aug 14 '12 at 12:17

4 Answers4

2

Are you sure you are getting a string object from the following code?

[d objectForKey:@"pubDate"]

Try logging the class of the object returned from d . Otherwise the code seems alright.

Edit: Check the locale settings in your device: Go to Settings, General, International in your device. If Region Format is not set to English you will have to use the setLocale method in the dateformatter Check the accepted answer for this question for more info: How do you interpret dates with NSDateFormatter?

Community
  • 1
  • 1
Rakesh
  • 3,370
  • 2
  • 23
  • 41
  • The very first line of my code: "NSLog(@"Unformatted date: %@", [d objectForKey:@"pubDate"]);" logs it, and it displays correctly. – Edd Slipszenko Aug 14 '12 at 11:14
  • you are just logging the object. Log the class of the object. NSLog("%@",[d class]); The solution provided by iApple would work no matter what object you are passing. So check that out. – Rakesh Aug 14 '12 at 11:16
  • [ class] would log the class of the object. Note that you are not logging the type of the variable. – Rakesh Aug 14 '12 at 11:20
  • Okay, running that now. Will get back to you with the results in a few moments. Thanks – Edd Slipszenko Aug 14 '12 at 11:21
  • Okay, I added in: `NSLog(@"Unformatted date class type: %@", [[d objectForKey:@"pubDate"] class]);` which produced: `2012-08-14 13:21:30.720 (App name removed)[9039:707] Unformatted date class type: __NSCFString` – Edd Slipszenko Aug 14 '12 at 11:23
  • Extract the object returned into a `NSString` and then give the `NSString` object to date formatter. – Rakesh Aug 14 '12 at 11:34
  • Yes thank you, your edit is what fixed it, by adding in the line: `[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];`. Thank you! – Edd Slipszenko Aug 14 '12 at 12:05
1

I have paste your code and have done minor modifications as I don't have live data and it works fine. Below is that sample code...

NSString *strDate = @"Mon, 02 Apr 2012";
NSLog(@"Unformatted date: %@", strDate);

// Set the publication date as an NSDate
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"EEE, dd MMM yyyy"];
NSDate *publicationDate = [dateFormatter dateFromString:strDate];    
NSLog(@"Date object description: %@", [publicationDate description]);

Replace below line

[self setPublicationDate:[dateFormatter dateFromString:[NSString stringWithFormat:@"%@",[d objectForKey:@"pubDate"]]]];

with your existing code and check.

[self setPublicationDate:[dateFormatter dateFromString:[d objectForKey:@"pubDate"]]];
alloc_iNit
  • 5,173
  • 2
  • 26
  • 54
  • Okay, trying this now. Will get back to you with the results momentarily. – Edd Slipszenko Aug 14 '12 at 11:25
  • Thanks for you suggestion, but I changed the line to: `[self setPublicationDate:[dateFormatter dateFromString:[NSString stringWithFormat:@"%@", [d objectForKey:@"pubDate"]]]];` and it's still producing the same results. Have I done this correctly? – Edd Slipszenko Aug 14 '12 at 11:27
0

Maybe you are passing a date object to to dateFromString.

NSDate *rawDate = [d objectForKey:@"pubDate"];
NSString *stringDate = [dateFormatter stringFromDate:rawDate];
NSDate *newDate = [dateFormatter dateFromString:stringDate];

Also, you should check the result of the operation dateFromString: separately (not within the assign statement for publicationDate).

Mundi
  • 79,884
  • 17
  • 117
  • 140
0

The value for [d objectForKey:@"pubDate"] is not in proper date time format.

Used standard time format and got this output.. (took it as eg)

NSDateFormatter *dateFormatter = nil;
if (!dateFormatter)  
{  
    dateFormatter = [[NSDateFormatter alloc] init];  
    [dateFormatter setDateFormat: @"EEE, dd MMM yyyy"];  
}

NSLog(@"todays date: %@", [NSDate date]);  
NSLog(@"formated date: %@", [dateFormatter stringFromDate:[NSDate date]]);

Output:

todays date: 2012-08-14 11:23:37 +0000  
formated date: Tue, 14 Aug 2012
Tommy Grovnes
  • 4,126
  • 2
  • 25
  • 40
Ashwin Kumar
  • 622
  • 4
  • 7