I am always getting a null from the date formatter. I tried examples in all the other questions and didn’t help.
NSLog(@"Date is %@", summaryItem.SubmittedDate);
NSLog(@"Formatted Date is %@", [df stringFromDate: summaryItem.SubmittedDate ]);
BOOL isDate = [summaryItem.SubmittedDate isKindOfClass: [NSDate class]];
NSLog(@"Date %@ a date.", isDate ? @"is" : @"is not");
Looking at the definition of SubmittedDate you have
@property (nonatomic, retain) NSDate *SubmittedDate;
I am initializing the Date Fomatter as so
- (void) initDateFormatter {
if( df == nil ) {
df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"MM/dd/yyyy"];
[df setDateStyle:NSDateFormatterShortStyle];
}
}
- Output to Log:
- 2012-11-21 10:00:41.650 Mobile_v2[4093:f803] Date is 11/21/2012 10:00:41 AM
- 2012-11-21 10:00:41.651 Mobile_v2[4093:f803] Formatted Date is (null)
- 2012-11-21 10:00:41.681 Mobile_v2[4093:f803] Date is not a date.
Can anyone see what’s wrong? Is my date format incorrect?
Code in my SBJson class
NSString *newDate = (NSString *)[receivedObjects objectForKey:@"SubmittedDate"];
NSDate *submittedDate = [df dateFromString:newDate]; //(NSDate *)
if ((NSNull *) submittedDate == [NSNull null]) {
self.SubmittedDate = nil;
} else {
self.SubmittedDate = submittedDate;
}
Actual value is “11/21/2012 10:47:54 AM”
From your log summaryItem.SubmittedDate object is not of class NSDate.
First log the class of that object,
if the output is not NSDate, there is something wrong in implementation (.m file) in the class where
@property (nonatomic, retain) NSDate *SubmittedDate;is declared.